0

Pyhton3 Question about dictionaries and lists

Task = Create a dictionary with this information (Smith - 8,5 ; Peters - 7,5; Jones - 4,5; Morgan - 6,0; Owens - 6,0). Write a program which takes the name as input from a user, and prints out the name, grade,and rank in the class. The name should not be case sensitive (e.g., "peter", "PETER", "Peter", etc should all be recognized correctly). This is my code but when I input the new person's information then the list is not sorted in order. Can anyone help please ? students= {"Smith":8.5 , "Peters":7.5 , "Jones" :4.5 ,"Morgan" :6.0 , "Owens" :6.0} newstudent= input ("please enter your name").lower() gr = input ("please enter your grade ").lower() students[newstudent]= gr keylist = list( students.keys() ) keylist.sort(reverse= True) for key in keylist: print( "{}:{}".format( key, students[key] ) ) the output I get is: (inputted john as name and 7 as grade) john:7 Smith:8.5 Peters:7.5 Owens:6.0 Morgan:6.0 Jones:4.5

10th Dec 2019, 10:49 AM
Eda Bozkurt
Eda Bozkurt - avatar
1 Answer
+ 6
What ~ swim ~ said about sorting on ascii value is 100% correct. There are better workarounds, but a quick solution could be to write newstudent= input ("please enter your name").capitalize() Edit: just take note that you are not answering the question, you are adding to the dict, which in itself might be a valid thing to do, the question asks to enter a name that needs to be displayed.
10th Dec 2019, 11:15 AM
Louis
Louis - avatar