[英]KeyError when printing dictionary?
因此,我正在嘗試編寫一個程序,讓用戶輸入課程編號,然后獲取該課程的房間、講師和會議時間信息。 如果用戶輸入未找到/無效的課程,我希望能夠打印終止消息,但是當我嘗試時,它會出現 KeyError。 我曾嘗試使用 try/except 語句,但是當我執行代碼時出現了 TypeError。
room_dict = {
"CS101": {
"Room": "3004"
},
"CS102": {
"Room": "4501"
},
"CS103": {
"Room": "6755"
},
"NT110": {
"Room": "1244"
},
"CM241": {
"Room": "1411"
},
}
instructor_dict = {
"CS101": {
"Instructor": "Haynes"
},
"CS102": {
"Instructor": "Alvarado"
},
"CS103": {
"Instructor": "Rich"
},
"NT110": {
"Instructor": "Burkes"
},
"CM241": {
"Instructor": "Lee"
},
}
time_dict = {
"CS101": {
"Time": "8:00 a.m."
},
"CS102": {
"Time": "9:00 a.m."
},
"CS103": {
"Time": "10:00 a.m."
},
"NT110": {
"Time": "11:00 a.m."
},
"CM241": {
"Time": "1:00 p.m."
},
}
courses = {"CS101", "CS102", "CS103", "NT110", "CM241"}
dicts = {'Room':room_dict,'Instructor':instructor_dict,'Time': time_dict}
dash_count = 50
dashes = dash_count * "-"
print(f'College Course Locater Program')
print(f'Enter a course number below to get information')
get_course = input(f'Enter a course number: ')
print(dashes)
course_num = get_course
room = room_dict[get_course]
instructor = instructor_dict[get_course]
time = time_dict[get_course]
if course_num in courses:
print(f'The details for course {get_course} are: ')
print(f"Room: {room['Room']}")
print(f"Time: {time['Time']}")
print(f"Instructor: {instructor['Instructor']}")
else: print(course_num, '是無效的課程編號。')
基本上,我得到的 output 是:
College Course Locater Program
Enter a course number below to get information
Enter a course number: CS # where CS is an invalid course number
--------------------------------------------------
Traceback (most recent call last):
File "C:\Users\Python\Downloads\courseinfo.py", line 75, in <module>
room = room_dict[get_course]
KeyError: 'CS'
我正在尋找的是:
College Course Locater Program
Enter a course number below to get information
Enter a course number: CS
--------------------------------------------------
(course number) is an invalid course number. # where (course number) is an invalid course number
多個問題:
首先,您檢查if get_course == room_dict:
。 但是room_dict
是整個字典! 它永遠不能等於您輸入的課程編號。
其次,不需要循環for item in room_dict.items():
。 字典中的值應該使用它們的鍵來訪問。 所以你會做類似的事情
get_course = input(f'Enter a course number: ')
print(f'The details for course {get_course} are: ')
room = room_dict[get_course]
instructor = instructor_dict[get_course]
time = time_dict[get_course]
print(f"Room: {room['Room']}, Time: {time['Time']}, Instructor: {instructor['Instructor']}")
當然,您需要確保get_course
作為鍵存在於所有這些詞典中。 你可以這樣做
if get_course in room_dict:
或者,try:
room = room_dict[get_course]
instructor = instructor_dict[get_course]
time = time_dict[get_course]
except KeyError:
print("Course info not found")
更好的是,您可以重新定義您的字典,以便一個字典包含有關課程的所有信息。
course_info = {
"CS101": {
"Room": "3004",
"Instructor": "Haynes",
"Time": "8:00 a.m."
},
"CS102": {
"Room": "4501",
"Instructor": "Alvarado",
"Time": "9:00 a.m."
}
# and so on
}
然后簡單地做:
get_course = input(f'Enter a course number: ')
try:
crs = course_info[get_course]
print(f'The details for course {get_course} are: ')
print(f"Room: {crs['Room']}, Time: {crs['Time']}, Instructor: {crs['Instructor']}")
except KeyError:
print(f"Details not found for {get_course}")
下面的代碼應該會讓你的生活更輕松。
它使用course_name = 'CM241'
僅用於演示目的。
room_dict = {
"CS101": {
"Room": "3004"
},
"CS102": {
"Room": "4501"
},
"CS103": {
"Room": "6755"
},
"NT110": {
"Room": "1244"
},
"CM241": {
"Room": "1411"
},
}
instructor_dict = {
"CS101": {
"Instructor": "Haynes"
},
"CS102": {
"Instructor": "Alvarado"
},
"CS103": {
"Instructor": "Rich"
},
"NT110": {
"Instructor": "Burkes"
},
"CM241": {
"Instructor": "Lee"
},
}
time_dict = {
"CS101": {
"Time": "8:00 a.m."
},
"CS102": {
"Time": "9:00 a.m."
},
"CS103": {
"Time": "10:00 a.m."
},
"NT110": {
"Time": "11:00 a.m."
},
"CM241": {
"Time": "1:00 p.m."
},
}
dicts = {'Time':time_dict,'Room':room_dict,'Instructor': instructor_dict}
course_name = 'CM241'
for k,v in dicts.items():
print(f'{k} --> {v[course_name][k]}')
output
Time --> 1:00 p.m.
Room --> 1411
Instructor --> Lee
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.