有某种字典问题

时间:2017-04-18 01:29:33

标签: python dictionary

我正在尝试让我的程序提示用户输入课程编号并获得有关该课程编号的多个答案。

    def main():
    courses = {}
course= {'CS101' : ['Room Number: ' '3004', 'Instructor: ' 'Haynes', 'Meeting Time: ' '8:00 a.m.'],

              'CS102' : ['Room Number: ' '4501', 'Instructor: ' 'Alvarado', 'Meeting Time: ' '9:00 a.m.'],
              'CS103' : ['Room Number: ' '6755', 'Instructor: ' 'Rich', 'Meeting Time: ' '10:00 a.m.'],
              'NT110' :['Room Number: ' '1244', 'Instructor: ' 'Burke', 'Meeting Time: ' '11:00 a.m.'],
              'CM241' :['Room Number: ' '1411', 'Instructor: ' 'Lee', 'Meeting Time: ' '1:00 p.m.'],
              }

search_course = str(input('Enter course number: '))
for key, value in course.items():
    print(key, value)

main()

提示用户输入课程编号。他应该得到房间号码,教练和会议时间,如下:

enter a course number: CS101
   Room Number 6755, Instructor: haynes, meeting time: 8:00 am.

相反,我得到了这个:

Enter course number: cs101
CS101 ['Room Number: 3004', 'Instructor: Haynes', 'Meeting Time: 8:00 a.m.']
CS102 ['Room Number: 4501', 'Instructor: Alvarado', 'Meeting Time: 9:00 a.m.']
CS103 ['Room Number: 6755', 'Instructor: Rich', 'Meeting Time: 10:00 a.m.']
NT110 ['Room Number: 1244', 'Instructor: Burke', 'Meeting Time: 11:00 a.m.']
CM241 ['Room Number: 1411', 'Instructor: Lee', 'Meeting Time: 1:00 p.m.']

如何让课程编号与我的成绩相等?

2 个答案:

答案 0 :(得分:1)

def main():
    courses = {
        'CS101' : ['Room Number: ' '3004', 'Instructor: ' 'Haynes', 'Meeting Time: ' '8:00 a.m.'],
        'CS102' : ['Room Number: ' '4501', 'Instructor: ' 'Alvarado', 'Meeting Time: ' '9:00 a.m.'],
        'CS103' : ['Room Number: ' '6755', 'Instructor: ' 'Rich', 'Meeting Time: ' '10:00 a.m.'],
        'NT110' : ['Room Number: ' '1244', 'Instructor: ' 'Burke', 'Meeting Time: ' '11:00 a.m.'],
        'CM241' : ['Room Number: ' '1411', 'Instructor: ' 'Lee', 'Meeting Time: ' '1:00 p.m.']
    }

    courseNum = raw_input("Enter course number:")
    print "you entered", courseNum

    for key, value in courses.items():
        if(key == courseNum.upper()):
            print ", ".join(value)
main()

<强>结果:

sh-4.3$ python main.py                                                                                                                                                      
Enter course number:NT110                                                                                                                                                   
you entered NT110                                                                                                                                                           
Room Number: 1244, Instructor: Burke, Meeting Time: 11:00 a.m.                                                                              

答案 1 :(得分:0)

由于每个字典键对应于该键的相关信息列表,因此问题似乎是正确分离并在每个列表中的字符串之间放置引号。

course_num = {'CS101': ['Room Number: 3004', 'Instructor: Haynes', 'Meeting Time: 8:00 a.m.'],
              'CS102': ['Room Number: 4501', 'Instructor: Alvarado', 'Meeting Time: 9:00 a.m.'],
              'CS103': ['Room Number: 6755', 'Instructor: Rich', 'Meeting Time: 10:00 a.m.'],
              'NT110': ['Room Number: 1244', 'Instructor: Burke', 'Meeting Time: 11:00 a.m.'],
              'CM241': ['Room Number: 1411', 'Instructor: Lee', 'Meeting Time: 1:00 p.m.']}

search_course = input('Enter course number: ').upper()
print(' '.join(course_num[search_course]))

演示:

Enter course number: CS101
Room Number: 3004 Instructor: Haynes Meeting Time: 8:00 a.m.

符合您要求的输出。