开发者

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',
                                 'CS开发者_运维问答103':'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.


You have three dictionaries (you should really only have one due to the repetition of the course code). However, based on what you currently have you need ensure that the course code exists in all three dictionaries.

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 in rooms and course in times and course in instructors:
        print('The details for course', course, 'are:')
        print('Room:', rooms[course])
        print('Instructor:', instructors[course])
        print('Time:', times[course])
    else:
        print('Invalid course')

# Call the main function.
main()

A better construct could be:

def main():
    courses = {'CS101': {'room': 3004, 'instructor': 'Haynes', 'time': '8:00 am'},
               'CS102': {'room': 4501, 'instructor': 'Alvarado', 'time': '9:00 am'},
               'CS103': {'room': 6755, 'instructor': 'Rich', 'time': '10:00 am'},
               'NT110': {'room': 1244, 'instructor': 'Burke', 'time': '11:00 am'},
               'CM241': {'room': 1411, 'instructor': 'Lee', 'time': '12:00 am'}}

    course = input('Enter a course number: ')

    if cd := courses.get(course):
        print('The details for course', course, 'are:')
        print('Room: {}\nInstructor: {}\nTime: {}'.format(*cd.values()))
    else:
        print('Invalid course')


# Call the main function.
main()


What stopping you replicate logic of intsructor as you done correctly. do same for rest.

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])
        print('Instructor:', instructors[course])
        print('Time:', times[course])

# Call the main function.
main()

Sample outputs #

Enter a course number:CS101
The details for course CS101 are:
Room: 3004
Instructor: Haynes
Time: 8:00 am


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜