python - The program will not let me get the last three scores of each student -
print("hello, , welcome maths quiz!") #asks user name name = input("what name? ") class_number = input("what class number? class 1, 2 or 3? please enter integer, no letters.") #this import random generate random functions import random #this variable score #it has been set 0 @ start of program score = 0 scores={} scores[name]=[] #this creates array containing mathematical operators #that quiz use ops = ['+','-','*'] #a loop has been set 0 - 10 x in range(10): #this variable has been set operator of equation #uses random function , choose random operator #array containing operators made earlier op = random.choice(ops) #the if statement checks if operation addition operation if op == '+': #this generate random number between 1 , 100 #for first integer first1 = random.randint(1,100) #this generate random number between 1 #and 100 first integer second1 = random.randint(1,100) #this print function generate mathematical question print ("what " + (str(first1) + op + str(second1) + "?")) #this eval function generate answer mathematical #question asked , store in answer variable answer = eval(str(first1) + op + str(second1)) #this loop try statement until integer entered guess variable while true: #the try statement see if guess variable given integer value, #if not print "you did not enter integer. not #valid answer try: #this allow user enter answer , #store in guess variable guess = int(input("")) #this break while loop if integer entered guess variable break except valueerror: print("you did not enter integer. not valid answer. please enter valid integer") print("answer quesiton appropiately. " + "what " + (str(first1) + op + str(second1)) + "?") if guess == answer: #if guess1 variable equal answer1 variable, #"correct!" printed , 1 point #added score print("correct!") score += 1 else: #if guess variable equal answer variable, #"correct!" printed , 1 point #added score #else "incorrect" printed print("incorrect") #the elif statement checks if operation subtraction operation elif op == '-': #this generate random number between 1 , 20 (because on 20 hard students) #for first integer , stores left2 variable first2 = random.randint(1,20) #this generate random number between 1 #and 20 (because on 20 hard students) second integer , stores right2 variable second2 = random.randint(1,20) #this print function generate mathematical question print ("what " + (str(first2) + op + str(second2) + "?")) #this eval function generate answer mathematical #question asked , store in answer1 variable answer1 = eval(str(first2) + op + str(second2)) #this loop try statement until integer entered guess1 variable while true: #the try statement see if guess variable given integer value, #if not print "you did not enter integer. not #valid answer try: #this allow user enter answer , #store in guess1 variable guess1 = int(input("")) #this break while loop if integer entered guess1 variable break except valueerror: print("you did not enter integer. not valid answer. please enter valid integer") print("answer question appropiately. " + "what " + (str(first2) + op + str(second2)) + "?") if guess1 == answer1: #if guess1 variable equal answer1 variable, #"correct!" printed , 1 point #added score print("correct!") score += 1 else: #else "incorrect" printed print ("incorrect") #the second elif statement checks if operation multiplication #operation elif op == '*': #this generates first number random number between #1 , 12 (because students tested on multiplication table) #used in multiplication calculation #and stores left3 variable first3 = random.randint(1,12) #this generates second number random number between #1 , 12 (because students tested on multiplication table) #used in multiplication calculation #and stores left3 variable second3 = random.randint(1,12) #this generates multiplcation question print ("what " + (str(first3) + op + str(second3) + "?")) #this creates answer multiplication question , #stores answer2 variable answer2 = eval(str(first3) + op + str(second3)) #this loop try statement until integer entered guess2 variable while true: #the try statement see if guess variable given integer value, #if not print "you did not enter integer. not #valid answer try: #this allows user enter own answer , store #the guess2 variable guess2 = int(input("")) #this break while loop if integer entered guess2 variable break except valueerror: print("you did not enter integer. not valid answer. please enter valid integer.") print("answer quesiton appropiately. " + "what " + (str(first3) + op + str(second3)) + "?") #the if statement checks if answer user entered equal #to answer of question if guess2 == answer2: #if guess2 variable equal answer2 variable, #"correct!" printed , 1 point #added score print("correct!") score += 1 else: #else "incorrect" printed print ("incorrect") else: #else if operation not addition, subtration or multiplcation #operation break statement enacted , #the whole if statement stop. however, #will unlikely happen since operation chosen #by program break #this print out user's score out of 10 print ("you got " + str(score) + "/10, " + name + " " + "from class " + class_number) #this create new variable connects both class_number variable , #the "class" string come out "class class_number". class_tag = "class "+ class_number #this create , open new text file under name #of class_tag variable. file = open(class_tag + ".txt", "a") #this write down user's name , score file.write(str(name) + " scored " + str(score)) #this create new line each user file.write("\n") #this close file. file.close() user_scores = {} user_scores[name].append(score) line in scores: name, score = line.rstrip('\n').split(' - ') score = int(score) if name not in user_scores or user_scores[name] < score: user_scores[name] = score name in sorted(user_scores): print(name, '-', user_scores[name]) print(user_scores[name][-3:])
i have created program allows students quiz , scores stored in text file based on class. i'm trying use text files data , order scores in terms of last three, user can use it. however, when tried:
user_scores = {} #this adds list of names user_scores[name].append(score) line in scores: name, score = line.rstrip('\n').split(' - ') score = int(score) if name not in user_scores or user_scores[name] < score: user_scores[name] = score name in sorted(user_scores): print(name, '-', user_scores[name]) print(user_scores[name][-3:])
the idle shell came with: user_scores[name].append(score) keyerror: 'student'
the class 0 text file's data is:
student scored 3 student scored 8 student scored 0 student scored 4 student scored 10 student scored 3 student scored 0 student scored 4
class 3 text file's data is:
katy scored 0 katy scored 2 katy scored 0 katy scored 6
user_scores
list full of lists, 1 list of scores per name.
update: tried:
user_scores = [] user_scores[name].append(score) line in scores: name, score = line.rstrip('\n').split(' - ') score = int(score) if name not in user_scores or user_scores[name] < score: user_scores[name] = score name in sorted(user_scores): print(name, '-', user_scores[name]) print(user_scores[name][-3:])
but idle shell stated:
user_scores[name].append(score) typeerror: list indices must integers, not str
so how program last 3 score's of student based on name? tried using many other methods , none of them have worked.
here:
user_scores = {} user_scores[name].append(score)
you trying append item list, user_scores[name]
not list, because user_scores empty dictionary.
if want quick , dirty want, then:
user_scores = {} line in scores: name, score = line.rstrip('\n').split(' scored ') score = int(score) if name not in user_scores: user_scores[name] = [] user_scores[name].append(score) name in sorted(user_scores.keys()): print(name + '-' + user_scores[name]) print(user_scores[name][-3:])
there multiple problems code, @ least give output want. kept print syntax used earlier in code consistency, mentioned should use other techniques.
Comments
Post a Comment