I am trying to create a dictionary that contains course information for a local school. it keeps giving me an error that there has to be an else statement after the if statement and i have that… please help!!
There are several syntax errors in your codes that may lead to strange error message:
if course not in rooms ← missing the colon ( : )
print(“The details for this Course”, (course), "are: ")) ← There is one extra “)” at the end, and “course” should not in parentheses
And there are two more issues:
The keys used in the three dictionaries are in string form, like ‘CS101’, ‘CS102’. When you use int function in the input statement to turn the response into interger, if the user enters ‘CS101’, it will resulted in value error; if the user just enters number like ‘101’, it will not have a corresponding key found in the dictionary “rooms”, and end up in the response “is an invalid Course Number”.
When you retrieve a value in a dictionary, square brackets should be used instead of parentheses, otherwise it will lead to error.
The following corrected lines should be okay:
course = input("Enter a Course Number: ") #get input from user to search for courses and their information
if course not in rooms: #loop through courses
print(course, "is an invalid Course Number")
#display corrseponding error message
else:#display course information
print("The details for this Course", course, "are: ")
print("Room: ", rooms[course])
print("Instructor: ", instructors[course])
print("Time: ", times[course])