tupitupa
tupitupa

Reputation: 21

Python: Unable to quit menu - user input

I need to define a function 'menu' for the user to be able to enter a number based on the choices. Here is what I have so far:

def menu(preamble, choices) :
    choose_one = " Choose one of the following and enter its number:"
    print(preamble + choose_one)

    for number in range(len(choices)):
        all = "%g: %s" % (number + 1, choices[number])
        print(all)

    prompt = ("\n")  
    warning = "\n"    
    while warning:
        choose = int(input(warning + prompt))
        warning = ''
        if choose not in range(1,len(choices)):
            warning = "Invalid response '%s': try again" % choose
            for number in range(len(choices)):
                all = "%g: %s" % (number + 1, choices[number])
                print(all)            
        else:
            break
    return choose

Let's say, for example that the choices are:

1. I have brown hair
2. I have red hair
3. Quit

I've tried running the code and it works fine when choose == 1 and choose == 2. But when choose == 3, it will ask the user to pick again. What do I need to do for the option "Quit" to work?

Upvotes: 0

Views: 61

Answers (1)

Padraic Cunningham
Padraic Cunningham

Reputation: 180532

You need to add 1:

len(choices) + 1

ranges are half open so the upper bound is not included so 3 is not in the range if the length is 3.

In [12]: l = [1,2,3]

In [13]: list(range(1, len(l)))
Out[13]: [1, 2]

In [14]: list(range(1, len(l) + 1))
Out[14]: [1, 2, 3]

I would also change your logic a bit:

st = set(range(1, len(choices))+ 1)
while True:
    choose = int(input(warning + prompt))
    if choose in st:
        return choose
    print("Invalid response '%s': try again" % choose)
    for number in range(1, len(choices)  + 1):
        msg = "%g: %s" % (number, choices[number])
        print(msg)    

You could also use a dict to store the choice and the number:

from collections import OrderedDict
choices  = OrderedDict(zip(range(1, len(choices)+ 1),choices))
for k,v in choices.items():
        msg = "%g: %s" % (k, v)
        print(msg)

while True:
    choose = int(input(warning + prompt))
    if choose in choices:
        return choose
    print("Invalid response '%s': try again" % choose)
    for k,v in choices.items():
        msg = "%g: %s" % (k, v)
        print(msg)

Upvotes: 1

Related Questions