How to define key in this code python -
i want code record latest 3 scores each student , when 4th 1 added overwrite oldest score , replace new one. need layout be:
f,7,8,9
i have created code when run it asks me define key. code:
pname = input("what name") correct = input("what score") score_filename = "class1.txt" max_scores = 3 try: scoresfile = open('class1.txt', "r+") except ioerror: scoresfile = open('class1.txt', "w+") # file not exists actualscorestable = dict() line in scoresfile: tmp = line.replace("\n","").split(",") actualscorestable[tmp[0]]=tmp[1:] scoresfile.close() if pname not in actualscorestable.keys(): actualscorestable[pname] = [correct] else: actualscorestable[pname].append(correct) if max_scores < len(actualscorestable[pname]): actualscorestable[key].pop(0) scoresfile = open(score_filename, "w+") # truncating file (write again) key in actualscorestable.keys(): scoresfile.write("%s,%s\n" % (key, ','.join(actualscorestable[key]))) scoresfile.close()
when run code tells me 'key' not defined. how define key? have been told needs tuple dont know how make , code work.
traceback (most recent call last): file "c:\users\milan\documents\python\task 3 tries\3 scores.py", line 23, in <module> actualscorestable[key].pop(0) nameerror: name 'key' not defined
the error in line:
actualscorestable[key].pop(0)
you don't define key until after code. think want actualscorestable[pname].pop(0)
.
Comments
Post a Comment