Search code examples
pythonlistinputargumentsline

Trying to have the corresponding answer by typing the room number


def main():
    # Initialize dictionaries
    rooms = { 'CS101':3004, 'CS102':4501, 'CS103':6755,
                        'NT110':1244, 'CM241':1411}
    
    instructors = {'CS101':'Haynes', 'CS102':'Alvarado',
                                 'CS103':'Rich', 'NT110':'Burke',
                                 'CM241':'Lee'}
    times = {'CS101':'8:00 am', 'CS102':'9:00 am',
                     'CS103':'10:00 am', 'NT110':'11:00 am',
                     'CM241':'12:00 pm'}
    course = input('Enter a course number:' )
    
    if course not in rooms:
        print(course, 'is an invalid course number.')
    else:
        print('The details for course', course, 'are:')
        print('Room:', rooms)
        print('Instructor:', instructors[course])
        print('Time:', times)

# Call the main function.
main()

Once I write the corresponding course number I should get the corresponding answer, instead I get everything.


Solution

  • The problem is that when you are trying to print out the dictionary, you are not accessing the value and instead trying to print out the whole dictionary in some cases.

    Here's how you can fix this:

    def main():
        # Initialize dictionaries
        rooms = {'CS101': 3004, 'CS102': 4501, 'CS103': 6755, 'NT110': 1244, 'CM241': 1411}
        instructors = {'CS101': 'Haynes', 'CS102': 'Alvarado', 'CS103': 'Rich', 'NT110': 'Burke', 'CM241': 'Lee'}
        times = {'CS101': '8:00 am', 'CS102': '9:00 am', 'CS103': '10:00 am', 'NT110': '11:00 am', 'CM241': '12:00 pm'}
        course = input('Enter a course number:')
        if course not in rooms:
            print(course, 'is an invalid course number.')
        else if course not in instructors:
            print(course, 'is an invalid course number.')
        else if course not in times:
            print(course, 'is an invalid course number.')
        else:
            print('The details for course', course, 'are:')
            print('Room:', rooms[course])
            print('Instructor:', instructors[course])
            print('Time:', times[course])
    
    
    main()
    

    Hope this helps!