Reputation: 99
How can i do a while loop to incorporate ValueError and input not in list.
def region():
print("What is the Region?")
print("1: Central")
print("2: North")
print("3: North-East")
print("4: East")
print("5: West")
region = int(input("Region: "))
while region not in [1,2,3,4,5]:
print("Invalid Response. Please choose the correct region.")
print("Please choose again!")
region = int(input("Region: "))
this code will keep looping if input is not within the list of options but if i were to input characters or symbols it will raise ValueError.
Upvotes: -2
Views: 42
Reputation: 442
def region():
print("What is the Region?")
print("1: Central")
print("2: North")
print("3: North-East")
print("4: East")
print("5: West")
reg = int(input("Region: "))
while reg not in [1,2,3,4,5]:
try:
print("Invalid Response. Please choose the correct region.")
print("Please choose again!")
reg = int(input("Region: "))
except Exception as e:
continue
return reg
Upvotes: 1