当用户参加此测验三次时,我如何对他们获得的分数进行排序,从最高分到最低分?另外,当代码运行时,我如何按字母顺序对用户名进行排序,并将其分数放在其名称旁边?
school_data = []
for x in range (0,3):
quiz = dict()
print ("Enter your name")
quiz['name'] = input()
print ("what class")
quiz['class_code'] = input()
print("1. 9+10=")
answer = input()
answer = int(answer)
if answer == 19:
print("correct")
score = score + 1
else:
print("wrong")
print("2. 16+40=")
answer = input()
answer = int(answer)
if answer == 56:
print("correct")
score = score + 1
else:
print("wrong")
print("3. 5+21=")
answer = input()
answer = int(answer)
if answer == 26:
print("correct")
score = score + 1
else:
print("wrong")
print("4. 5-6=")
answer = input()
answer = int(answer)
if answer == -1:
print("correct")
score = score + 1
else:
print("wrong")
print("5. 21-9=")
answer = input()
answer = int(answer)
if answer == 12:
print("correct")
score = score + 1
else:
print("wrong")
quiz['score'] = score
school_data.append(quiz)发布于 2016-03-01 04:45:39
您可以使用以下命令按分数对school_data列表进行排序
sorted_school_data = sorted(school_data, key=lambda k: k['score'])默认情况下,首先对分数最低的进行排序,所以从最高到最低只需这样做
sorted_school_data = sorted(school_data, key=lambda k: k['score'])[::-1]要打印分数和姓名,您可以执行以下操作
for i in sorted(school_data, key=lambda k: k['name']):
print('%s:%s' %(i['name'], i['score']))https://stackoverflow.com/questions/35708378
复制相似问题