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:
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!