Write a Python program that creates a dictionary containing course numbers and the room numbers of the rooms where the course meets. The dictionary should have the following key-value pairs: Course Number (key) Room Number (value) CS101 3004 CS102 4501 CS103 6755 NT110 1244 CM241 1411 The program should also create a dictionary containing course numbers and the names of the instructors that teach that course. The dictionary should have the following key-value pairs: Course Number (key) Instructor (value) CS101 Haynes CS102 Alvarado CS103 Rich NT110 Burke CM241 Lee The program should also create a dictionary containing course numbers and the meeting times for each course. The dictionary should have the following key-value pairs: Course Number (key) Meeting Time (value) CS101 8:00 am CS102 9:00 am CS103 10:00 am NT110 11:00 am CM241 1:00 pm The program should let the user enter a course number, then it should display the course’s room number, instructor, and meeting time. Ask the user multiple times until he or she decides to exit (y / n).

Respuesta :

Limosa

Answer:

Following are the program in the Python Programming Language:

#set dictionary

rooms ={}

rooms.update({'CS101' : 3004})

rooms.update({'CS102' : 4501})

rooms.update({'CS103' : 6755})

rooms.update({'NT110' : 1244})

rooms.update({'CM241' : 1411})

#set dictionary

instructors={}

instructors.update({'CS101' : 'Haynes'})

instructors.update({'CS102' : 'Alvarado'})

instructors.update({'CS103' : 'Rich'})

instructors.update({'NT110' : 'Burke'})

instructors.update({'CM241' : 'Lee'})

#set dictionary

time={}

time.update({'CS101' : '8:00 a.m'})

time.update({'CS102' : '9:00 a.m'})

time.update({'CS103' : '10:00 a.m'})

time.update({'NT110' : '11:00 a.m'})

time.update({'CM241' : '1:00 p.m'})

while True:

 n=input("You want to continue(y/n): ")

 if(n=="n"):

   break

 else:

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

   try:

     print()

     print("Room Number: ", rooms[cours])

     print("Instructors: ",instructors[cours])

     print("Meeting time: ", time[cours])

   except KeyError:

     print("Invalid")

Output:

You want to continue(y/n): y

Enter a course number: CS101

Room Number:  3004

Instructors:  Haynes

Meeting time:  8:00 a.m

You want to continue(y/n): y

Enter a course number: NT1244

Invalid

You want to continue(y/n): y

Enter a course number: NT110

Room Number:  1244

Instructors:  Burke

Meeting time:  11:00 a.m

You want to continue(y/n): n

Explanation:

Here we define three dictionary type variables "rooms", "instructors", "time"

and assign value in these dictionaries by the update() method.

Then, we set the while loop inside it:

  • set a variable "n" and get input from the user.
  • set the if conditional statement and pass the condition is the variable "n" is equal to the value "n" then, loop is break otherwise,
  • we get an input from the user in the variable "cours" and apply try block inside it, we print "room number", "Instructor", "Meeting time" as given in the output and in except block it print "Invalid".