Need menu choices that grow with any list? Python’s range() creates a sequence of integers for a loop. With range(1, len(choices) + 1), the numbers begin at 1 and stop before the final boundary, so adding 1 includes the menu’s length. section 4.0
Build the allowed answers
For three drinks, range(1, len(choices) + 1) produces 1, 2, and 3. A for loop converts each number to text and appends it to allowed_answers, letting the menu validate user input. section 4.0
. . .
while True:
choice = input(prompt)
allowed_answers = [] #1
for a in range(1, len(choices)+1): #2
allowed_answers.append(str(a)) #3
if choice not in allowed_answers:
answer = choices[int(choice) - 1]
break
else:
print("Enter number 1, 2, or 3!")
answer = ''
. . .
See range() used inside a menu loop to create valid numeric answers from the list length. section 4.0







