Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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:
        print('The details for course', course, 'are:')
        print('Room:', rooms[course]) // This has been changed to access the corresponding rooms value instead of printing out the whole rooms dictionary
        print('Instructor:', instructors[course])
        print('Time:', times[course])

# Call the main function.
main()

Hope this helps!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading