简体   繁体   English

使用用户输入从字典中的一个键打印多个值

[英]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.我需要这样做,以便当用户输入课程名称时,它会打印所有 3 个值。

For example, if the user says CS101, then it should print [3004, 'Haynes', '8:00 AM']例如,如果用户说 CS101,那么它应该打印 [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])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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