当用户输入1时,如何移出第一个if语句?

时间:2016-07-31 13:26:22

标签: python-3.x

user_input = input(menu).strip().lower()
#This program reads student names and grades
while True:#FIXME
    if user_input == '1':#FIXME incorrect output
         name,grades = input(prompt).split()#Add key and value to dict
         student_grades[name] = grades
         print(name)
    elif user_input == '2':#delete name from dict
         del_name = input('Enter name to delete: ')
         del student_grades[del_name]
    elif user_input == '3': #print names and grades
         for key, value in student_grades.iteritems():
             print(key, value)
    elif user_input == '4':
         break
    else:
         print('Unkown Command')
         break

1 个答案:

答案 0 :(得分:0)

您陷入第一个条件if语句的原因是代码中的逻辑错误。您要求并且用户输入等于' 1'然后开始一个无限的while循环。在此while循环中,您不再要求用户输入,因此它始终等于' 1'并且您不断运行第一个if语句的内容。要修复移动第一行,要求用户输入到while循环。像这样:

#This program reads student names and grades
while True:#FIXME
    user_input = input(menu).strip().lower()
    if user_input == '1':#FIXME incorrect output
    [...]