Home > Blockchain >  I cant pick a choice after looping
I cant pick a choice after looping

Time:06-05

def print_menu():
    print('1. American')
    print('2. Asian')
    print('3. Indian')
    print('4. Mexican')
    print('5. French')
    print('6. Italian')
    print('7. Seafood')
    print('8. Pizza')
print_menu()
menu = input('\nChoose where you want to eat from-->')
if menu == "1":
    def american_menu():
        print('1. Dempsey Burger Pub')
        print('2. Redrock Canyon Grill-Wichita')
        print("3. Cheddar's Scratch Kitchen")
        print("4. Neighbors| Restaurant & Bar")
        print("5. The Kitchen")
        print("6. Firebirds Wood Fired Grill")
        print("7. Chicken and Pickle")
    american_menu()
    american = input("\nChoose which American Restaurant--> ")
    if american == "1":
        print("\nCall Dempsey Burger Pub")
        

    while True:
        go_back = input("Will you like to try another menu option?: ")
        if go_back == "Yes":
            print_menu()
        else:
            print("We'll continue with your current choice")
        break

so i tried looping it so it goes back to choose again from the Cuisines and moving on to where you want to eat but so far, it just asks the go_back, after i say yes...it keeps repeating the go_back again any help will be appreciated. Thanks. i want it to loop back to the choices, pick the choice and sub-choice i selected rather than it just picking the choice and not do anything. Thanks again

NB:this is an assignment and i am stuck plus i had a list of the choices but couldnt post it due to the site.

CodePudding user response:

Write a while loop for print_menu and change continue with break


def print_menu():
    while True:
    #Use 2 while loops if you want. But the loop you are using is not needed  

while True:
    go_back = input("Will you like to try another menu option?: ")
    if go_back == "Yes":
        print_menu()
        break
    else:
        print("We'll continue with your current choice")
        break

Second Option:


def print_menu():
    while True:
    #Just use "while" here instead of the second one

go_back = input("Will you like to try another menu option?: ")
if go_back == "Yes":
    print_menu()
else:
   print("We'll continue with your current choice")

CodePudding user response:

You don't need to use a while True loop at all. It might cause problems with infinite looping. You can avoid it by calling a new function check_if_wants_to_order_again after a user has chosen his menu in print_menu().

def print_menu():
  print('1. American')
  print('2. Asian')
  print('3. Indian')
  print('4. Mexican')
  print('5. French')
  print('6. Italian')
  print('7. Seafood')
  print('8. Pizza')
  menu = input('\nChoose where you want to eat from-->')
  if menu == "1":
    american_menu()
  check_if_wants_to_order_again()

def american_menu():
  print('1. Dempsey Burger Pub')
  print('2. Redrock Canyon Grill-Wichita')
  print("3. Cheddar's Scratch Kitchen")
  print("4. Neighbors| Restaurant & Bar")
  print("5. The Kitchen")
  print("6. Firebirds Wood Fired Grill")
  print("7. Chicken and Pickle")
  american = input("\nChoose which American Restaurant--> ")
  if american == "1":
      print("\nCall Dempsey Burger Pub")

def check_if_wants_to_order_again():
  go_back = input("Will you like to try another menu option? Enter \"Yes\" or \"No\":")
  if go_back == "Yes":
    print_menu()
  else:
    print("We'll continue with your current choice")

print_menu()

CodePudding user response:

When you continue your loop, you're calling print_menu() again, but you aren't doing any of the other things that need to follow print_menu() in order to prompt the user to make another choice.

Give this a shot:

main_menu = {
    "1": "American",
    "2": "Asian",
    "3": "Indian",
    "4": "Mexican",
    "5": "French",
    "6": "Italian",
    "7": "Seafood",
    "8": "Pizza",
}

american_menu = {
    "1": "Dempsey Burger Pub",
    "2": "Redrock Canyon Grill-Wichita",
    "3": "Cheddar's Scratch Kitchen",
    "4": "Neighbors| Restaurant & Bar",
    "5": "The Kitchen",
    "6": "Firebirds Wood Fired Grill",
    "7": "Chicken and Pickle",
}

country_menus = {
    "1": american_menu,
}

def print_menu(menu):
    for k, v in menu.items():
        print(f"{k}. {v}")

while True:
    print_menu(main_menu)
    pick = input('\nChoose where you want to eat from-->')
    if pick not in main_menu:
        print("Please select one of the listed options.")
        continue
    if pick not in country_menus:
        print("Oops!  Haven't implemented that one yet.")
        print(f"Try {', '.join(country_menus)}")
        continue

    country, country_menu = main_menu[pick], country_menus[pick]
    print_menu(country_menu)
    menu = input(f"\nChoose which {country} Restaurant--> ")
    if pick not in country_menu:
        print("Sorry, not an option.  Let's start over.")
        continue
    print(f"\nCall {country_menu[pick]}")

    pick = input("Will you like to try another menu option?: ")
    if pick != "Yes":
        print("We'll continue with your current choice")
        break

Note that all of the interaction happens inside the while True loop. Any time there's a continue within that loop, it goes back to the print_menu(main_menu) at the start, and then continues from there.

  • Related