how do you find and print a dictionary keyvalue that matches user input?

This code here is unnecessary, because you are essentially setting 4 variables all to the same value get_course:

course_num = get_course
number = course_num
name = course_num
meeting = course_num 

This code here doesn’t work because you are trying to find a key with string “room” in a dictionary that doesn’t exist, and same with the other lines afterwards

print(f'Room: {number["room"]}')
print(f'Instructor: {name["instructor"]}')
print(f'Time: {meeting["time"]}')

I replaced the code above with this:

print(f'Room: {room[get_course]}')
print(f'Instructor: {instructor[get_course]}')
print(f'Time: {time[get_course]}')

This searches the dictionary variable room for the key get_course (ex. “CS101”) and returns the value corresponding to that key. The same thing happens for the other lines, except with the dictionary instructor and the dictionary time.

Here is the final code:

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')

get_course = input(f'Enter course number here: ')

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]}')
    print(f'{get_course} is an invalid course number.')

Here is a test with the input “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.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top