【问题标题】:Trying to have the corresponding answer by typing the room number通过输入房间号尝试得到相应的答案
【发布时间】:2022-12-07 16:11:33
【问题描述】:
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()

一旦我写下相应的课程编号,我应该得到相应的答案,而不是我得到了一切。

【问题讨论】:

    标签: python list input arguments line


    【解决方案1】:

    您有三本词典(由于课程代码重复,您实际上应该只有一本)。但是,根据您当前拥有的内容,您需要确保课程代码存在于所有三个词典中。

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

    更好的构造可能是:

    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:', cd['room'])
            print('Instructor:', cd['instructor'])
            print('Time:', cd['time'])
        else:
            print('Invalid course')
    
    
    # Call the main function.
    main()
    

    【讨论】:

    • 只有 8 秒的差距@cobra 发布了 smae ..HAhah
    • 这应该是一个有效的答案......因为我们正在链接 3 个具有相同键的不同字典。在访问它们之前必须进行检查 +1
    【解决方案2】:

    问题是,当您尝试打印出字典时,您并没有访问该值,而是在某些情况下尝试打印出整个字典。

    以下是解决此问题的方法:

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

    希望这可以帮助!

    【讨论】:

    • 我试过了,我得到了 ValueError: could not convert string to float:
    • 嗯,这很奇怪。你能告诉我你使用的输入法吗?
    • 我试着输入 CS101
    • 哦,我想我看到了问题!我会更新我的答案。等一下 :)
    • 感谢您的回复,我不敢相信我错过了这样简单的事情。
    【解决方案3】:

    是什么阻止你在正确完成时复制 intsructor 的逻辑。做同样的休息。

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

    样本输出#

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

    【讨论】:

    • 您需要检查所有三个词典。课程 ID 可能存在于房间但不在导师或者
    猜你喜欢
    • 2021-03-04
    • 2019-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多