为什么每次在此代码中运行“退出”都会收到关键错误?

时间:2019-04-24 05:23:43

标签: python python-3.x

当系统提示您输入课程号或输入退出以退出时,我输入“ exit”,但它一直给我一个关键错误。预期的结果应该是打印“程序正常终止”,但是却出现了一个关键错误。

instructor = {'CS101': 'Haynes', 'CS102': 'Alvarado', \'CS103': 'Rich','NT110': 'Burke', 'CM241': 'Lee'}

room = {'CS101': '3004', 'CS102': '4501', \'CS103': '6755', 'NT110': '1244', 'CM241': '1411'} 

time = {'CS101': '8:00 a.m.', 'CS102': '9:00 a.m.', \'CS103': '10:00 a.m.', 'NT110': '11:00 a.m.', 'CM241': '1:00 p.m.'}
exit = 0

while exit != 1:

course = input("Enter the course number or enter exit to leave: ").upper()

print()

print("The course information for course # " + course + " is as follows:")
print('Room #: ', room[course])
print('Instructor: ', instructor[course])
print('Time: ', time[course])
if course.upper() == "exit":
    exit = 1
    print("Program terminated normally")

2 个答案:

答案 0 :(得分:0)

代码中的一些问题。
1.可以先在if条件下检查退出条件,然后再检查其他逻辑。现在,该循环在您键入exit之后,循环再次运行,导致KeyError
2.您应该检查course.lower() == 'exit'而不是upper()
3.代码中出现意外的反斜杠

下面的代码将起作用,并且具有上面提到的修复程序

instructor = {'CS101': 'Haynes', 'CS102': 'Alvarado', 'CS103': 'Rich','NT110': 'Burke', 'CM241': 'Lee'}

room = {'CS101': '3004', 'CS102': '4501', 'CS103': '6755', 'NT110': '1244', 'CM241': '1411'}

time = {'CS101': '8:00 a.m.', 'CS102': '9:00 a.m.', 'CS103': '10:00 a.m.', 'NT110': '11:00 a.m.', 'CM241': '1:00 p.m.'}
exit = 0

while exit != 1:

    course = input("Enter the course number or enter exit to leave: ").upper()

    print()
    #lower instead of upper
    if course.lower() == "exit":
        exit = 1
        print("Program terminated normally")
    else:
        print("The course information for course # " + course + " is as follows:")
        print('Room #: ', room[course])
        print('Instructor: ', instructor[course])
        print('Time: ', time[course])

输出看起来像

Enter the course number or enter exit to leave: CS101

The course information for course # CS101 is as follows:
Room #:  3004
Instructor:  Haynes
Time:  8:00 a.m.
Enter the course number or enter exit to leave: exit

Program terminated normally

此外,您可以使用sys.exit()退出代码,而不必像这样检查变量exit

instructor = {'CS101': 'Haynes', 'CS102': 'Alvarado', 'CS103': 'Rich','NT110': 'Burke', 'CM241': 'Lee'}

room = {'CS101': '3004', 'CS102': '4501', 'CS103': '6755', 'NT110': '1244', 'CM241': '1411'}

time = {'CS101': '8:00 a.m.', 'CS102': '9:00 a.m.', 'CS103': '10:00 a.m.', 'NT110': '11:00 a.m.', 'CM241': '1:00 p.m.'}

import sys

while True:

    course = input("Enter the course number or enter exit to leave: ").upper()

    print()
    if course.lower() == "exit":
        print("Program terminated normally")
        #This will exit the code
        sys.exit()
    else:
        print("The course information for course # " + course + " is as follows:")
        print('Room #: ', room[course])
        print('Instructor: ', instructor[course])
        print('Time: ', time[course])

答案 1 :(得分:0)

在创建字典(4501', \'CS103)时,代码中存在转义键的问题,为什么需要此反斜杠?

您还缺少while循环的缩进块。

但是,要点到了:如果在输入“退出”后浏览代码,则首先要转到:

print("The course information for course # " + course + " is as follows:")
print('Room #: ', room[course])
print('Instructor: ', instructor[course])
print('Time: ', time[course])

您尝试使用room[course]获取课程属性,如果key不在字典中,则会抛出KeyError,此处课程“ exit”在room中不是有效的。