In: Computer Science
I am working on making a simple gradebook. How it works is it asks user for name and grade. It does this for multiple instances until the person types quit to end the program. The code I have here works. My question is how do I print the output in alphabetical order?
Output from the input I put into my program:
David 98
Annabelle 87
Josh 91
Ben 95
What I want my code to do (in alphabetical order):
Annabelle87
Ben 95
David 98
Josh 91
Here is my code for the program:
grades = {}
def set_grade(name, grade):
grades[name] = grade
def print_grades_names():
for i in grades.keys():
print('{} {}'.format(i, str(grades[i])))
while True:
name = input("Enter a name (or 'quit' to stop): ")
if name == 'quit':
break
grade = input("Enter a grade: ")
set_grade(name, grade)
(print_grades_names())
Please look at my code and in case of indentation issues check the screenshots.
---------------main.py-------------------------
grades = {}
def set_grade(name, grade):
grades[name] = grade
def print_grades_names():
for i in sorted(grades.keys()):
#use sorted, to sort the
keys(names)
print('{} {}'.format(i,
str(grades[i]))) #print name and
grade
while True:
name = input("Enter a name (or 'quit' to stop):
")
if name == 'quit':
break
grade = input("Enter a grade: ")
set_grade(name, grade)
print_grades_names()
--------------Screenshots-----------------------
------------------Output----------------------
-----------------------------------------------------------------------------------------------------------
Please give a thumbs up if you find this answer helpful.
If it doesn't help, please comment before giving a thumbs
down.
Please Do comment if you need any clarification.
I will surely help you.