Need help with looping back through my statement if input is 'not in' a group of dictionaries (SOLVED!)

I’m sorry, I am a complete noob to programming and the python language. I am in my first intro CS class and I cannot figure out how to get a loop to restart in the following code following the last elif statement:

Course_Room = {
‘CS101’: ‘3004’,
‘CS102’: ‘4501’,
‘CS103’: ‘6755’,
‘NT110’: ‘1244’,
‘CM241’: ‘1411’
}
Course_Instructor = {
‘CS101’: ‘Haynes’,
‘CS102’: ‘Alvarado’,
‘CS103’: ‘Rich’,
‘NT110’: ‘Burke’,
‘CM241’: ‘Lee’
}
Course_Meeting = {
‘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.’
}

def main():
# Create a variable for the user input looking up a course.
course = input('Enter the course number you are trying to look up. ')

if course in Course_Room and Course_Instructor and Course_Meeting:
    print(f'Course Room # is: {Course_Room[course]}.')
    print(f'Course Instructor is: {Course_Instructor[course]}')
    print(f'Course Meeting time is: {Course_Meeting[course]}')

elif course not in Course_Room or Course_Instructor or Course_Meeting:
    print('Please enter a valid course number.')

if name == ‘main’:
main()

Also this is my very first post and if this not the proper channel to be posting these types of questions can someone please direct me to the proper channel for beginner style problems and questions? Thanks in advance.

By Andre via Discussions on Python.org at 12May2022 02:38:

I’m sorry, I am a complete noob to programming and the python language.
I am in my first intro CS class and I cannot figure out how to get a
loop to restart in the following code following the last elif
statement:

Create a variable for the user input looking up a course.

course = input('Enter the course number you are trying to look up. ')

if course in Course_Room and Course_Instructor and Course_Meeting:
print(f’Course Room # is: {Course_Room[course]}.‘)
print(f’Course Instructor is: {Course_Instructor[course]}’)
print(f’Course Meeting time is: {Course_Meeting[course]}')

elif course not in Course_Room or Course_Instructor or Course_Meeting:
print(‘Please enter a valid course number.’)

Well, you want a loop which keeps asking the question until a valid
answer is obtained. That lets you express the loop condition: when does
the loop run? When we do not yet have a valid answer.

So something shaped like this should work:

valid = False    # we have not received a valid course yet
while not valid:
    course = input('Enter the course number you are trying to look up.  ')
    if course in Course_Room and Course_Instructor and Course_Meeting:
        valid = True
    else:
        print('Please enter a valid course number.')

# at this point we know the answer is valid
print(f'Course Room # is: {Course_Room[course]}.')
print(f'Course Instructor is: {Course_Instructor[course]}')
print(f'Course Meeting time is: {Course_Meeting[course]}')

BTW, your test isn’t correct: I don’t actually know your formal
criteria, but if you’re trying to test whether the course number is
listed in all 3 of Course_Room, Course_Instructor and Course_Meeting you
need to write that like this:

if (
    course in Course_Room
    and course in Course_Instructor
    and course in Course_Meeting
):

The brackets and line breaks are just for readability.

Yes, the Users category is the right place for this post. There’s some
discussion about making a distinct “Python Help” category, whcih would
be the place for this, but that has not yet happened.

Cheers,
Cameron Simpson cs@cskk.id.au

1 Like

Hello, @Chefman_14, and welcome to Python Software Foundation Discourse!

Notice that the same five keys appear in all three of your dictionaries. Therefore, you only need to test whether what the user entered appears as a key in one of them. This would accomplish that:

if course in Course_Room:

If the condition evaluates to True, you can be confident that the user’s response appears as a key in all of the dictionaries. On the other hand, if it evaluates to False, it does not appear as a key in any of them.

1 Like

I think that EAFP (Easier to Ask for Forgivness than Permission) approach could be applied: inside while True loop try to print and break; in case of KeyError print appropriate message.

1 Like

Thanks Cameron so much for your help with this.
This is a huge help, I knew it had something to do with the ‘while’ loop but we are just learning the basics of dictionaries and access information from them and it was really confusing me on how I access the values associated with them.

Thanks again for your help! :smiley:

1 Like

Thanks Quercus for your help and input! I appreciate all the help I can get! :smiley:

1 Like