Home > Software engineering >  How would I be able to return to main menu?
How would I be able to return to main menu?

Time:10-04

I've been making a simple inventory database script that runs in the CLI and uses if/else statements based off user input. My problem is after it queries the SQL database it prompts the user if they would like to make the same query again or return to the main menu and select a different query. I'm able to run the query code again using a while loop and continue statement but when I select the "Return to main menu" option it seemingly works by displays the main menu options and prompts the user for a choice but once the user enter their choice it has no effect and displays the menu a second time which then works for the user to choose a different query. I've tried things like using if/else instead of while statements and played around with it a little more with no results.

Here's the code:

   # Main Menu Options
def mainmenu():
    print("(1) Add (Brand) New Item to Inventory")
    print("(2) Remove Item from Inventory")
    print("(3) Update Inventory")
    print("(4) Print Current Inventory Report")
    print("(5) '99' to quit")
    print("_" * 32)
    global choice_1
    choice_1 = (int(input("Enter Choice:")))

while True:
        mainmenu()

    # (1)
        if choice_1 == 1:
         while True:

            print("What item would you like to add? (eg. 'keyboards', 'laptops', etc.)")
            add_item = (input("Enter Choice:")).casefold()
            print("Enter quantity of {}:".format(add_item))
            quantity = (int(input()))

        # Data Insertion
            query = "INSERT into items(devicetype,quantity)VALUES (?,?)"
            data = (add_item, quantity)
            cursor.execute(query,data)
            con.commit()
            if(cursor.execute(query,data)):
                print("Added {} {} to Inventory successfully.".format(quantity, add_item))
            else:
                print("Could not add {} to Inventory.".format(add_item))
            #cursor.close()
            # Main menu options
            print()
            print("Would you like to add another new item to invenotry or return to main menu?")
            print()
            print("(1) Return to main menu:")
            print("(2) Add another item to inventory:")
            print()
            main_menu_choice_1 = (int(input("Enter choice here:")))

            if main_menu_choice_1 == 1:
                mainmenu()
            else:
                if main_menu_choice_1 == 2:
                    continue



            break

CodePudding user response:

Near the bottom of the code you posted, change

if main_menu_choice_1 == 1:
    mainmenu()

to

if main_menu_choice_1 == 1:
   break

Why does this work? Because you have two while loops, an outer one and an inner one. This break statement breaks you out of the inner while loop, and so the program continues with iterating the outer while loop. And the first line of that outer loop calls mainmenu().

  • Related