'exit(), conditionals, return or break on Python menu?

If we are doing a menu on python and the user selects option for finish the interaction. Is preferable to use exit(), conditionals, return or break?

Example with break, where we stop the infinite loop with the break:

    def show_menu():
        print('1. Pet kitten\n'
              '0. Exit')


    def start_app():
        while True:
            show_menu()
            user_choice = input('Select an option: ')
            if user_choice == '1':
                pet()
            elif user_choice == '0':
                print('\nBye!')
                break
            else:
                print('\nPlease select a number from the menu.')


    start_app()

Example with exit(), where we use built-in function exit() for stop the execution of the script:

    def show_menu():
        print('1. Pet kitten\n'
              '0. Exit')


    def start_app():
        while True:
            show_menu()
            user_choice = input('Select an option: ')
            if user_choice == '1':
                pet()
            elif user_choice == '0':
                print('\nBye!')
                exit()
            else:
                print('\nPlease select a number from the menu.')


    start_app()

Example with conditionals, where the while stops when the condition change:

    def show_menu():
        print('1. Pet kitten\n'
              '0. Exit')


    def start_app():
        continue_ = True
        while continue_:
            show_menu()
            user_choice = input('Select an option: ')
            if user_choice == '1':
                pet()
            elif user_choice == '0':
                print('\nBye!')
                continue_ = False
            else:
                print('\nPlease select a number from the menu.')


    start_app()

Example with return, where we finish the interaction returning a random value:

    def show_menu():
        print('1. Pet kitten\n'
              '0. Exit')


    def start_app():
        continue_ = True
        while continue_:
            show_menu()
            user_choice = input('Select an option: ')
            if user_choice == '1':
                pet()
            elif user_choice == '0':
                print('\nBye!')
                return None
            else:
                print('\nPlease select a number from the menu.')


    start_app()


Solution 1:[1]

It is worth noticing that your options fall into two categories, with one important difference between them.

On one hand, you can use break, return or a condition variable to break out of the loop and, eventually, return to the caller. Among these options, I'd say just pick whichever gives the cleanest code.

On the other hand, you can use exit() which ends the program there and then.

If you ever wish to use this as something other than a top-level menu, for example wrapped in a library to be used as a sub-menu of something else, you do not want the program to suddenly exit.

Generally, exit() is a rather big chunk of explosives, that should be treated with a bit of respect.

Solution 2:[2]

For example 1: First give 4 space in line 2 so that the print function can stay into the show_menu() function and second define the #pet() function in line 11, otherwise you will get name error. Then put a break statement after line 11.

For example 3: The while loop stop in line 15 when you define continue as False.

#HappyCoding

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ture PÄlsson
Solution 2 Rafiul Islam