[Python] Exiting back to main menu

Discussão em 'Python' iniciado por Stack, Setembro 14, 2024.

  1. Stack

    Stack Membro Participativo

    I am currently writing a basic program with multiple menus. I have a function called menu1(), which is called in main and handles the first set of menu options. My issue I am having is being able to return to main() from my menu1() function. In menu1() I have a while loop which continues to display the menu after the user has inputted their selection so that they don't have to restart the program every time. If I include a return statement in menu1() it just returns to the start of menu1() and displays those menu prompts again. Here is my menu1() function, which is called the main:

    def menu1(Stock:list[StockItem]):
    """
    Handles the Stock menu user selections
    """
    choice:str = ""
    print("")
    print("Stock menu")
    print("")
    print("1. Add new stock")
    print("2. Remove stock from system")
    print("3. Add quantity to existing stock")
    print("4. Remove quantity from existing stock")
    print("5. Display all stock")
    print("6. Return to previous menu")
    print("")
    choice = input("Please select option: ")

    while choice not in ["1", "2", "3", "4", "5", "6"]:
    print("Invalid selection")
    choice = input("Please select option: ")

    while choice != "6":
    match choice:
    case "1":
    add_stock(Stock)
    return
    case "2":
    remove_stock(Stock)
    return
    case "3":
    item = input("Enter stock item name to add quantity: ").upper()
    amount = input_int("Enter quantity to add: ")
    update_existing_stock(Stock, item, amount)
    return
    case "4":
    item = input("Enter stock item name to remove quantity: ").upper()
    amount = input_int("Enter quantity to remove: ")
    update_existing_stock(Stock, item, -amount)
    return
    case "5":
    display_all_stock(Stock)
    return
    case "6":
    return


    I have tried return and break statements however neither have worked. Does anyone have any suggestions?

    Continue reading...

Compartilhe esta Página