我有关于学生和他/她的成绩的列表。我想要两件事。其中一个是首选;添加学生和他/她的成绩。其中第二个是第二选择;按字母顺序退出并排序所有学生。
答案中的真实算法。
答案 0 :(得分:0)
grades[student] = hishergrade
。您没有定义变量student
和hishergrade
,而是使用name
和phone
。 因此,您的代码应修改为:
def print_menu():
print('1. Add a student and his/her grade')
print('2. Quit and Sort')
grades = {}
menu_choice = 0
print_menu()
while menu_choice != 2:
menu_choice = int(input("Type in a number (1-2): "))
if menu_choice == 1:
print("Add Student and His/Her Grade")
name = input("Student: ")
phone = input("Grade: ")
grades[name] = phone #Use the right variables
elif menu_choice == 2:
print("Grades:")
for x in sorted(grades.keys()): #Sort
print("Student: ", x, "\tGrade:", grades[x])
print()
print_menu()
答案 1 :(得分:-1)
我不是百分之百确定你在问什么,但也许这就是你需要的东西?
print("Grades:")
keys = list(grades.keys())
keys.sort()
for x in keys:
print("Student: ", x, "\tGrade:", grades[x])
print()
print_menu()