如何从多个词典中获取相同键的值?

时间:2015-11-15 20:24:16

标签: python dictionary key

我必须显示用户输入的课程的房间号,讲师和会议时间。这一切都必须用字典来完成。我尝试了很多东西,但是我无法让代码工作。

def main():

print('Enter a class: ')
user_class = input()

class_name, ia = classes()

for i in range (5):

    if user_class.lower() == class_name.lower():
        print(room_numb[ia])
    else:
        print('That class does not exist.')

字典:

def classes():


room_numb = {'CS101' : "3004",
             'CS102' : "4501",
             'CS103' : "6755",
             'NT110' : "1244", 
             'CM241' : "1411"}


instructor = {'CS101' : 'Haynes', 
              'CS102' : 'Alvarado',
              'CS103' : 'Rich',
              'NT110' : 'Burke',
              'CM241' : 'Lee'}


meet_time = {'CS101' : '8:00 a.m.', 
             'CS102' : '9:00 a.m.', 
             'CS103' : '10:00 a.m.', 
             'NT110' : '11:00 a.m.', 
             'CM241' : '1:00 p.m.'}
return room_numb, instructor, meet_time

main()

1 个答案:

答案 0 :(得分:0)

room_numb = {'CS101' : "3004",
             'CS102' : "4501",
             'CS103' : "6755",
             'NT110' : "1244", 
             'CM241' : "1411"}


instructor = {'CS101' : 'Haynes', 
              'CS102' : 'Alvarado',
              'CS103' : 'Rich',
              'NT110' : 'Burke',
              'CM241' : 'Lee'}


meet_time = {'CS101' : '8:00 a.m.', 
             'CS102' : '9:00 a.m.', 
             'CS103' : '10:00 a.m.', 
             'NT110' : '11:00 a.m.', 
             'CM241' : '1:00 p.m.'}

user_class = input('Enter a class: ').upper()

print('room_numb:', room_numb.get(user_class, "not exists"))
print('instructor:', instructor.get(user_class, "not exists"))
print('meet_time:', meet_time.get(user_class, "not exists"))