Reputation: 23
This is my dictionary that has multiple values assigned to one key
courseinfo = {'CS101': [3004, 'Haynes', '8:00 AM'],
'CS102': [4501, 'Alvarado', '9:00 AM'],
'CS103': [6755, 'Rich', '10:00 AM'],
'NT110': [1244, 'Burke', '11:00 AM'],
'CM241': [1411, 'Lee', '1:00 PM']
}
I need to make it so that when the user enters a course name, it prints all 3 values.
For example, if the user says CS101, then it should print [3004, 'Haynes', '8:00 AM']
I've tried a few different ways, but none of them seem to work:
x = input("enter course name:")
for key, value in courseinfo.items():
if x == key:
print(key)
key = input("enter course name:")
if key in courseinfo:
print(courseinfo[key])
x = input('enter course name:')
for i in x:
print(courseinfo[i])
Upvotes: 1
Views: 360
Reputation: 118
Just get the value in the dictionary by the key:
courseinfo = {'CS101': [3004, 'Haynes', '8:00 AM'],
'CS102': [4501, 'Alvarado', '9:00 AM'],
'CS103': [6755, 'Rich', '10:00 AM'],
'NT110': [1244, 'Burke', '11:00 AM'],
'CM241': [1411, 'Lee', '1:00 PM']
}
x = input("enter course name:")
print(courseinfo[x])
Upvotes: 2