简体   繁体   中英

printing multiple values from a key in a dictionary with user input

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM