# Standard multi choice question template
def multiChoiceQuestion(options: list):
while True:
print("\nEnter the number of your choice - ")
for x in range(len(options)):
print(str((x + 1)) + ". " + options[x])
print("\n")
try:
answer = int(input())
except ValueError:
print("Doesn't seem like a number! Try again!")
continue
if answer < 1 or answer > len(options):
print("That option does not exist! Try again!")
continue
return answer
I created a template to ask a multi choice question in python. The loop will never reach it's end, since there is always a continue or a return statement. Is the while True
condition appropriate for it?
while True
condition appropriate for it?" That depends on whether that is the intended behaviour. Is it? \$\endgroup\$ – Mast Mar 7 '19 at 17:29for x in len(options):
will produce an error as Python doesn't allow iteration over an integer. 200_success's approach is the way to go here, but for future reference, usefor x in range(len(options)):
if you really need to loop a certain number of times. This takes thelen(options)
integer and creates an interable out of it. \$\endgroup\$ – JDG Mar 7 '19 at 19:32