-1

I am currently working on an ordering meal system using python and the task is to let user select the meal options that are available on the menu. It should allow the user to enter a meal selection number (which matches the number shown in the menu) and the appropriate validation should be performed on user response

After a valid choice the program should print “now cooking [name]”

Otherwise it should print “invalid choice”.

def customer_selection():
    menu_items = ["Budda Bowl(vg)", "Eye Fillet Steak",
                  "Spaghetti Bolognese", "Pad Thai(seafood)"]
    user_input = int(input())
    if(user_input in menu_items):
        print('Now cooking',menu_item[user_input-1])
    else:
        print('invalid choice.')

print(customer_selection())
Jane
  • 7
  • 1
  • If the user *chooses* '1' (`user_input = 1`) then what do you expect `user_input in menu_items` evaluates to? – wwii Apr 02 '22 at 14:18
  • If you are using an IDE **now** is a good time to learn its debugging features Or the built-in [Python debugger](https://docs.python.org/3/library/pdb.html). Printing *stuff* at strategic points in your program can help you trace what is or isn't happening. [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – wwii Apr 02 '22 at 14:19

3 Answers3

2

The issue is the line user_input in menu_items. The in operator with a list is referring to the actual items in the list -- not their indices.

If you want use indices like that, you can use a dict:

def customer_selection():
    menu_items = {k:v for k,v in enumerate(["Budda Bowl(vg)", "Eye Fillet Steak", "Spaghetti Bolognese", "Pad Thai(seafood)"],1)}
    user_input = int(input())
    if user_input in menu_items :
        print('Now cooking',menu_items[user_input])
    else:
        print('invalid choice.')
        
customer_selection()

Another way is to replace your if/else with try/except to catch the bad index of a list:

def customer_selection():
    menu_items = ["Budda Bowl(vg)", "Eye Fillet Steak", "Spaghetti Bolognese", "Pad Thai(seafood)"]
    user_input = int(input())
    try:
        print('Now cooking',menu_items[user_input-1])
    except IndexError:
        print('invalid choice.')
        
customer_selection()
dawg
  • 98,345
  • 23
  • 131
  • 206
0

Your test user_input in menu_items means "is user_input a element of the array menu_items" : not at all, you don't want to exactly type an item, what would be the point

So as you input an index, you need to verify that it is lower than the array size. As arrays are 0-indexed, do int(input()) - 1 do let the user enter a 1-indexed value

def customer_selection():
    menu_items = ["Budda Bowl(vg)", "Eye Fillet Steak",
                  "Spaghetti Bolognese", "Pad Thai(seafood)"]
    user_input = int(input()) - 1
    if user_input < len(menu_items):
        print('Now cooking', menu_items[user_input])
    else:
        print('invalid choice.')

customer_selection()
azro
  • 53,056
  • 7
  • 34
  • 70
0

The in operator checks whether a specified value is a constituent element of a list. Since user_input is not present in the menu_items ,customer_selection function always executes else part.

def customer_selection():
    menu_items = ["Budda Bowl(vg)", "Eye Fillet Steak", "Spaghetti Bolognese", "Pad Thai(seafood)"]
    user_input = int(input())
    if user_input>0 and user_input<=len(menu_items):
        print('Now cooking '+menu_items[user_input-1])
    else:
        print('invalid choice.')
customer_selection()
Ashish M J
  • 642
  • 1
  • 5
  • 11