1

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])
Mhluzi Bhaka
  • 1,364
  • 3
  • 19
  • 42
oh my god
  • 23
  • 1
  • 2
  • 7
  • Try to go through this link and see if it helps. The answer from @vjsr should help you but it does not provide enough documentation on why. This link should help you get a better understanding of why. https://stackoverflow.com/questions/45072283/how-to-use-a-python-dictionary Also see https://www.w3schools.com/python/python_dictionaries.asp – Joe Ferndz Nov 24 '20 at 04:31

1 Answers1

2

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

Dimitri
  • 118
  • 1
  • 8