2016-02-29 61 views
0

如何將用戶獲得的分數從最高分到最低分三次進行測試?另外,如何按照字母順序對用戶名進行排序,並在代碼運行時將他們的分數與他們的姓名相鄰?如何對測驗完成3次時獲得的分數進行排序?

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) 
+0

W你有沒有試過的帽子?您存儲測驗結果,分數和用戶的數據結構是什麼?測驗本身的代碼與這個問題並不特別相關。 –

回答

0

您可以

sorted_school_data = sorted(school_data, key=lambda k: k['score']) 

此默認排序與最低得分在前排序得分您school_data列表,以便從最高到只是做

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'])) 
相關問題