-1

Please note that this is on Python 3.3

Here is the code:

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)

Here is the outcome of the code:

>>> 
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
>>> 

I want the code to add the three scores of the students and sort the total scores of the students, with the according name.

For example: (2 Students)

Student 1

  • Score 1 - 2
  • Score 2 - 4
  • Score 3 - 7

(Therefore the total is 13)

Student 2

  • Score 1 - 5
  • Score 2 - 1
  • Score 3 - 4

(Therefore the total is 10)

(The program prints in order from highest to lowest)

"Student 1 - 15 , Student 2 - 10"

Anonymous
  • 49
  • 1
  • 1
  • 9

1 Answers1

0

You need to use the syntax key=final_score when passing a function to sort by:

b.sort(key=final_score)

But the sort method expects a function to be passed in not a variable so passing the int value from adding name2 + name3 + name4 is not going to work.

If you just want the list of scores sorted simply call b.sort()

What you should be doing is using a defautdict and using each name as key and store all scores in a list:

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()))

To show mean, total and max it is trivial:

# 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 sorted by highest user total score:

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

Now you have a dict where the student names are the keys and each students scores are all stored in a list.

Really you should add a try/except taking the user input and verify it is in the correct format.

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • That doesn't work unfortunately. Here is the outcome: – Anonymous Feb 05 '15 at 15:25
  • 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 What is your name? test Enter your first score: 1 Enter your second score: 2 Enter your third score: 3 What is your nametest 2 Enter your first score: 3 Enter your second score: 4 Enter your third score: 5 12 Traceback (most recent call last): File "H:\GCSE Computing\Task 3\Task 3.py", line 31, in b.sort(key = final_score) TypeError: 'int' object is not callable >>> – Anonymous Feb 05 '15 at 15:26
  • @ThifyanRavinehru, yes you need to pass a function as the key, how can python sort using an int? – Padraic Cunningham Feb 05 '15 at 15:27
  • So, it is not possible to do what I wanted to do (sorting int variables) ? – Anonymous Feb 05 '15 at 15:28
  • Is there a way to do it without variables, because I just want the 3 scores to add up and the total to be sorted. – Anonymous Feb 05 '15 at 15:29
  • no `final_score = name2 + name3 + name4` is an int how could you sort just one number? – Padraic Cunningham Feb 05 '15 at 15:29
  • Okay, I guess I'll do enter your total score. Thank you for your help, I will just wait a bit to check if anyone else knows a different way. If not, I will vote yours as best answer. – Anonymous Feb 05 '15 at 15:31
  • I added a way to simply store all names and all their scores. All you have to do is sum the list values to get total scores and sort to get the highest – Padraic Cunningham Feb 05 '15 at 15:39
  • 'if options == ("SCORES HIGH TO LOW") or options == ("SCORE HIGH TO LOW"): 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 – Anonymous Feb 10 '15 at 14:39
  • 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))) – Anonymous Feb 10 '15 at 14:39
  • it says invalid syntax and highlights the b from 'b[name]' – Anonymous Feb 10 '15 at 14:40
  • sorry i didn't know about the code format here is my code: – Anonymous Feb 10 '15 at 14:43
  • `if options == ("SCORES HIGH TO LOW") or options == ("SCORE HIGH TO LOW"): 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` – Anonymous Feb 10 '15 at 14:43
  • `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)))` – Anonymous Feb 10 '15 at 14:44
  • I know why I was getting the error, its because there wasn't a closed bracket after: – Anonymous Feb 10 '15 at 15:00
  • `scores = input("Enter your scores separated by a space: "` Thank you very much for the help Padraic – Anonymous Feb 10 '15 at 15:01