2015-02-05 23 views
-1

Please note that this is on Python 3.3如何對變量中的整數進行排序?

下面是代碼:

students=int(input("How many student's score do you want to sort? ")) 
options=input("What do you want to sort: [Names with scores] , [Scores high to low] , [Scores averages] ? ") 
options=options.upper() 

if options == ("NAMES WITH SCORES") or options == ("NAME WITH SCORE") or options == ("NAME WITH SCORES") or options == ("NAMES WITH SCORE"): 
    a=[] 
    for i in range(0,students): 
     name=input("Enter your scores and name: ") 
     a.append(name) 

    a.sort() 
    print("Here are the students scores listed alphabetically") 
    print(a) 

if options == ("SCORES HIGH TO LOW") or options == ("SCORE HIGH TO LOW"): 
    b=[] 
    number=0 
    for i in range(0,students): 
     number = number+1 
     print("Student "+str(number)) 
     name2=int(input("Enter your first score: ")) 
     name3=int(input("Enter your second score: ")) 
     name4=int(input("Enter your third score: ")) 

     b.append(name2) 
     b.append(name3) 
     b.append(name4) 

    final_score = name2 + name3 + name4 
    print (final_score) 
    b.sort(final_score) 
    print("Student "+str(number)) 
    print(b) 

這裏是代碼的結果:

>>> 
How many student's score do you want to sort? 2 
What do you want to sort: [Names with scores] , [Scores high to low] , [Scores averages] ? scores high to low 
Student 1 
Enter your first score: 1 
Enter your second score: 2 
Enter your third score: 3 
Student 2 
Enter your first score: 3 
Enter your second score: 5 
Enter your third score: 6 
14 
Traceback (most recent call last): 
    File "H:\GCSE Computing\Task 3\Task 3.py", line 31, in <module> 
    b.sort(final_score) 
TypeError: must use keyword argument for key function 
>>> 

我想要的代碼中加入了學生和排序的三個分數學生的總分數,以及相應的名稱。

例如: (2名學生)

學生1

  • 分數1 - 2
  • 分數2 - 4
  • 評分3 - 7

(因此總數是13)

學生2

  • 分數1 - 5
  • 分數2 - 1
  • 分數3 - 4

(因此總是10)

(爲了從最高到最低的程序打印)「學生1 - 15,學生2 - 10」

回答

0

您需要使用語法key=final_score傳遞功能時,排序:

b.sort(key=final_score)

但那種方法需要一個function傳遞不可變所以從添加name2 + name3 + name4傳遞int值是不會噸工作。

如果你只是想整理簡單地調用得分列表b.sort()

你應該在列表中使用defautdict並使用每個名稱作爲鍵,存儲所有的分數做:

from collections import defaultdict 


d = defaultdict(list) 

for _ in range(students): 
    name = input("Enter your name: ") 
    scores = input("Enter your scores separated by a space: " 
    # add all scores for the user to the list 
    d[name].extend(map(int,scores.split())) 

要顯示平均,總和最大實在是小巫見大巫:

# from statistics import mean will work for python 3.4 

for k,v in d.items(): 
     print("Scores total for {} is {}".format(k,sum(v))) 
     print("Scores average for {} is {}".format(k,sum(v)/len(v))) # mean(v) for python 3,4 
     print("Highest score for {} is {}".format(k, max(v))) 

打印由最高的用戶總得分排序:

print("The top scoring students from highest to lowest are:") 
for k,v in sorted(d.items(),key=lambda x:sum(x[1]),reverse=True): 
    print("{} : {}".format(k,sum(v))) 

現在你有一個字典,學生的名字是關鍵,每個學生的成績都存儲在一個列表中。

真的,你應該添加一個嘗試/除了採取用戶輸入,並驗證它是在正確的格式。

+0

這不起作用。這裏是結果: – Anonymous 2015-02-05 15:25:33

+0

你想排序多少個學生的分數? 2 你想排序什麼:[有得分的名字],[得分高到低],[得分平均值]?得分從高到低 你叫什麼名字?測試 輸入您的第一得分:1 輸入您的第二得分:2 輸入第三得分:3 你是什麼nametest 2 輸入第一得分:3 輸入第二得分:4 輸入您的第三得分:5 回溯(最近最後調用): 文件 「H:\ GCSE計算\任務3 \任務3.py」,第31行,在 b.sort(鍵= final_score) 類型錯誤: 'INT' 目的是不可回調 >>> – Anonymous 2015-02-05 15:26:04

+0

@ThifyanRavinehru,是的,你需要傳遞一個函數作爲關鍵,python如何使用int進行排序? – 2015-02-05 15:27:27

相關問題