Steven Wright
Steven Wright

Reputation: 13

Menu Created in Python Using While Loop Doesn't Exit When Wanted

I'm new to Python and have been trying to create a simple menu that exits the program when the user presses 4. The menu works if the user immediately selects 4, but if the user selects other options before pressing 4, the while loop doesn't break and the menu continues. Any ideas how I can change it so the menu does exit properly?

def main(): 
     print('''
           Welcome to Grade Central
           [1] - Enter Grades
           [2] - Remove Student
           [3] - Student Average Grades
           [4] - Exit
           \n
           ''')

     action = int(input("What would you like to do today? \n"))
     if action == 1:
         print(1)
     elif action == 2:
         print(2)
     elif action == 3:
         print(3)
     elif action == 4:
         print("The program has been exited")
     else:
         print("Invalid input! Please select an option")
     return action

while main() != 4:
     main()

Upvotes: 1

Views: 1928

Answers (4)

Jesper
Jesper

Reputation: 1649

You call main() twice for each loop and only test one of the return values.

Use this instead:

while main() != 4:
     pass

pass is a command that does nothing.

Upvotes: 0

gmdev
gmdev

Reputation: 3155

I would suggest some form of input validation in your program.

def main():
    action = get_input()
    # do something with action

def get_input():
    action = 0
    while action not in range(1,5):
        try:
            action = int(input('What would you like to do today?\n'))
            if action not in range(1,5):
                print('Action not recognized.')
        except ValueError:
            print('Please enter an integer.')

    return action

This allows you to check if the user input was an integer and/or it is an action that you handle. It will keep asking the user, "What would you like to do today?" until the input is valid. You can modify the range to handle more values, and you can modify the error messages to display a help output.

Upvotes: 0

Kyle Hardinge
Kyle Hardinge

Reputation: 95

If you create a variable outside of the function that is set to whatever is returned from the function and have that getting checked by the while loop, that should work.

def main():
    print('''
           Welcome to Grade Central
           [1] - Enter Grades
           [2] - Remove Student
           [3] - Student Average Grades
           [4] - Exit
           \n
           ''')

    action = int(input("What would you like to do today? \n"))
    if action == 1:
        print(1)
    elif action == 2:
        print(2)
    elif action == 3:
        print(3)
    elif action == 4:
        print("The program has been exited")
    else:
        print("Invalid input! Please select an option")
    print(action)
    return action

returned_action = 0
while returned_action != 4:
    returned_action = main()

Upvotes: 1

Jordan Simba
Jordan Simba

Reputation: 1216

def main(): 
     print('''
           Welcome to Grade Central
           [1] - Enter Grades
           [2] - Remove Student
           [3] - Student Average Grades
           [4] - Exit
           \n
           ''')

     action = int(input("What would you like to do today? \n"))
     if action == 1:
         print(1)
     elif action == 2:
         print(2)
     elif action == 3:
         print(3)
     elif action == 4:
         print("The program has been exited")
     else:
         print("Invalid input! Please select an option")
     return action

action = main()
while action != 4:
     action = main()

Upvotes: 1

Related Questions