如何查找并打印与用户输入匹配的字典 key/value?
How to find and print a dictionary key/value that matches user input?
我需要打印一个与用户输入匹配的字典值。例如,如果用户输入课程编号 CS101
,输出将如下所示:
The details for CS101 are:
Room: 3004
Instructor: Haynes
Time: 8:00 a.m.
但是,如果用户输入 incorrect/invalid 课程编号,我需要打印一条消息让他们知道:
CS101 is an invalid course number.
我已经尝试了 if
、for loops
和 while loops
。问题是,每次我打印课程信息时,由于 KeyError,无效课程编号消息不会显示。另一方面,如果我碰巧“修复”了错误消息,那么课程编号信息将不会打印出来,而是 return 一个 NameError / TypeError。
老实说,我已经为此苦苦挣扎了一段时间,我觉得我要么分配不正确,要么打印不正确。但是我是初学者,我还没有很好地掌握 Python ,这就是我寻求帮助的原因。
不幸的是,不允许我创建一个完整的字典来对所有内容进行分组(这对我来说会更容易),而是我必须创建 3 个字典。
这是代码:
room = {}
room["CS101"] = "3004"
room["CS102"] = "4501"
room["CS103"] = "6755"
room["NT110"] = "1244"
room["CM241"] = "1411"
instructor = {}
instructor["CS101"] = "Haynes"
instructor["CS102"] = "Alvarado"
instructor["CS103"] = "Rich"
instructor["NT110"] = "Burkes"
instructor["CM241"] = "Lee"
time = {}
time["CS101"] = "8:00 a.m."
time["CS102"] = "9:00 a.m."
time["CS103"] = "10:00 a.m."
time["NT110"] = "11:00 a.m."
time["CM241"] = "1:00 p.m."
def info():
print(f'College Course Locater Program')
print(f'Enter a course number below to get information')
info()
get_course = input(f'Enter course number here: ')
print(f'----------------------------------------------')
course_num = get_course
number = course_num
name = course_num
meeting = course_num
if number in room:
if name in instructor:
if meeting in time:
print(f'The details for course {get_course} are: ')
print(f'Room: {number["room"]}')
print(f'Instructor: {name["instructor"]}')
print(f'Time: {meeting["time"]}')
else:
print(f'{course_num} is an invalid course number.')
我也试过用这种风格格式化字典:
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."
},
}
我提前感谢所有对解决方案有建议、回答或建议的人。
此处的代码是不必要的,因为您实质上是将 4 个变量都设置为相同的值 get_course:
course_num = get_course
number = course_num
name = course_num
meeting = course_num
此处的代码不起作用,因为您试图在不存在的字典中查找带有字符串“room”的键,之后的其他行也是如此
print(f'Room: {number["room"]}')
print(f'Instructor: {name["instructor"]}')
print(f'Time: {meeting["time"]}')
我将上面的代码替换为:
print(f'Room: {room[get_course]}')
print(f'Instructor: {instructor[get_course]}')
print(f'Time: {time[get_course]}')
这会在字典变量 room 中搜索键 get_course(例如“CS101”)和 returns 对应于该键的值。除了字典讲师和字典时间之外,其他行也会发生同样的事情。
这是最终代码:
room = {}
room["CS101"] = "3004"
room["CS102"] = "4501"
room["CS103"] = "6755"
room["NT110"] = "1244"
room["CM241"] = "1411"
instructor = {}
instructor["CS101"] = "Haynes"
instructor["CS102"] = "Alvarado"
instructor["CS103"] = "Rich"
instructor["NT110"] = "Burkes"
instructor["CM241"] = "Lee"
time = {}
time["CS101"] = "8:00 a.m."
time["CS102"] = "9:00 a.m."
time["CS103"] = "10:00 a.m."
time["NT110"] = "11:00 a.m."
time["CM241"] = "1:00 p.m."
def info():
print(f'College Course Locater Program')
print(f'Enter a course number below to get information')
info()
get_course = input(f'Enter course number here: ')
print(f'----------------------------------------------')
if get_course in room and get_course in instructor and get_course in time:
print(f'The details for course {get_course} are: ')
print(f'Room: {room[get_course]}')
print(f'Instructor: {instructor[get_course]}')
print(f'Time: {time[get_course]}')
else:
print(f'{get_course} is an invalid course number.')
这是输入“CS101”的测试:
College Course Locater Program
Enter a course number below to get information
Enter course number here: CS101
----------------------------------------------
The details for course CS101 are:
Room: 3004
Instructor: Haynes
Time: 8:00 a.m.
你也可以这样做。它可能需要更少的时间。该功能不是很有条理,试着稍微组织一下,它应该可以工作。我对在这里添加代码还不是很熟悉。
course_info = {
'CS101': {
'Room': '3004',
'Instructor': 'Haynes',
'Time': '8:00 am'
},
'CS102': {
'Room': '4501',
'Instructor': 'Alvarado',
'Time': '9:00 a.m.'
},
'CS103': {
'Room': '6755',
'instructor': 'Rich',
'Time:': '10:00 am',
},
'NT110': {
'Room': '1244',
'instructor': 'Burkes',
'Time': '11:00 am'
},
'CM241': {
'Room': '1411',
'Instructor': 'Lee',
'Time': '1:00 pm'
},
}
get_course = input(f'Enter a course number: ')
try:
courses = course_info[get_course]
print(f'The details for for course {get_course} are: ')
print(f"Room: {courses['Room']}, Time: {courses['Time']},
Instructor: {courses['Instructor']}")
except KeyError:
print(f'Details not found for {get_course}')
我需要打印一个与用户输入匹配的字典值。例如,如果用户输入课程编号 CS101
,输出将如下所示:
The details for CS101 are:
Room: 3004
Instructor: Haynes
Time: 8:00 a.m.
但是,如果用户输入 incorrect/invalid 课程编号,我需要打印一条消息让他们知道:
CS101 is an invalid course number.
我已经尝试了 if
、for loops
和 while loops
。问题是,每次我打印课程信息时,由于 KeyError,无效课程编号消息不会显示。另一方面,如果我碰巧“修复”了错误消息,那么课程编号信息将不会打印出来,而是 return 一个 NameError / TypeError。
老实说,我已经为此苦苦挣扎了一段时间,我觉得我要么分配不正确,要么打印不正确。但是我是初学者,我还没有很好地掌握 Python ,这就是我寻求帮助的原因。
不幸的是,不允许我创建一个完整的字典来对所有内容进行分组(这对我来说会更容易),而是我必须创建 3 个字典。
这是代码:
room = {}
room["CS101"] = "3004"
room["CS102"] = "4501"
room["CS103"] = "6755"
room["NT110"] = "1244"
room["CM241"] = "1411"
instructor = {}
instructor["CS101"] = "Haynes"
instructor["CS102"] = "Alvarado"
instructor["CS103"] = "Rich"
instructor["NT110"] = "Burkes"
instructor["CM241"] = "Lee"
time = {}
time["CS101"] = "8:00 a.m."
time["CS102"] = "9:00 a.m."
time["CS103"] = "10:00 a.m."
time["NT110"] = "11:00 a.m."
time["CM241"] = "1:00 p.m."
def info():
print(f'College Course Locater Program')
print(f'Enter a course number below to get information')
info()
get_course = input(f'Enter course number here: ')
print(f'----------------------------------------------')
course_num = get_course
number = course_num
name = course_num
meeting = course_num
if number in room:
if name in instructor:
if meeting in time:
print(f'The details for course {get_course} are: ')
print(f'Room: {number["room"]}')
print(f'Instructor: {name["instructor"]}')
print(f'Time: {meeting["time"]}')
else:
print(f'{course_num} is an invalid course number.')
我也试过用这种风格格式化字典:
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."
},
}
我提前感谢所有对解决方案有建议、回答或建议的人。
此处的代码是不必要的,因为您实质上是将 4 个变量都设置为相同的值 get_course:
course_num = get_course
number = course_num
name = course_num
meeting = course_num
此处的代码不起作用,因为您试图在不存在的字典中查找带有字符串“room”的键,之后的其他行也是如此
print(f'Room: {number["room"]}')
print(f'Instructor: {name["instructor"]}')
print(f'Time: {meeting["time"]}')
我将上面的代码替换为:
print(f'Room: {room[get_course]}')
print(f'Instructor: {instructor[get_course]}')
print(f'Time: {time[get_course]}')
这会在字典变量 room 中搜索键 get_course(例如“CS101”)和 returns 对应于该键的值。除了字典讲师和字典时间之外,其他行也会发生同样的事情。
这是最终代码:
room = {}
room["CS101"] = "3004"
room["CS102"] = "4501"
room["CS103"] = "6755"
room["NT110"] = "1244"
room["CM241"] = "1411"
instructor = {}
instructor["CS101"] = "Haynes"
instructor["CS102"] = "Alvarado"
instructor["CS103"] = "Rich"
instructor["NT110"] = "Burkes"
instructor["CM241"] = "Lee"
time = {}
time["CS101"] = "8:00 a.m."
time["CS102"] = "9:00 a.m."
time["CS103"] = "10:00 a.m."
time["NT110"] = "11:00 a.m."
time["CM241"] = "1:00 p.m."
def info():
print(f'College Course Locater Program')
print(f'Enter a course number below to get information')
info()
get_course = input(f'Enter course number here: ')
print(f'----------------------------------------------')
if get_course in room and get_course in instructor and get_course in time:
print(f'The details for course {get_course} are: ')
print(f'Room: {room[get_course]}')
print(f'Instructor: {instructor[get_course]}')
print(f'Time: {time[get_course]}')
else:
print(f'{get_course} is an invalid course number.')
这是输入“CS101”的测试:
College Course Locater Program
Enter a course number below to get information
Enter course number here: CS101
----------------------------------------------
The details for course CS101 are:
Room: 3004
Instructor: Haynes
Time: 8:00 a.m.
你也可以这样做。它可能需要更少的时间。该功能不是很有条理,试着稍微组织一下,它应该可以工作。我对在这里添加代码还不是很熟悉。
course_info = {
'CS101': {
'Room': '3004',
'Instructor': 'Haynes',
'Time': '8:00 am'
},
'CS102': {
'Room': '4501',
'Instructor': 'Alvarado',
'Time': '9:00 a.m.'
},
'CS103': {
'Room': '6755',
'instructor': 'Rich',
'Time:': '10:00 am',
},
'NT110': {
'Room': '1244',
'instructor': 'Burkes',
'Time': '11:00 am'
},
'CM241': {
'Room': '1411',
'Instructor': 'Lee',
'Time': '1:00 pm'
},
}
get_course = input(f'Enter a course number: ')
try:
courses = course_info[get_course]
print(f'The details for for course {get_course} are: ')
print(f"Room: {courses['Room']}, Time: {courses['Time']},
Instructor: {courses['Instructor']}")
except KeyError:
print(f'Details not found for {get_course}')