python loop invalid input

Solutions on MaxInterview for python loop invalid input by the best coders in the world

showing results for - "python loop invalid input"
Noa
22 Nov 2018
1take input
2while incorrect input:
3    take input
4    
5#Eg. Taking the month input for the first quarter of the year.
6months = ['january', 'february', 'march']
7month = input('Select the month').lower()
8while month not in months:
9  month = input('Oops! Incorrect input. Select month again').lower()
10  
11
12  
Ida
24 Mar 2020
1while True:
2    try:
3        # Note: Python 2.x users should use raw_input, the equivalent of 3.x's input
4        age = int(input("Please enter your age: "))
5    except ValueError:
6        print("Sorry, I didn't understand that.")
7        #better try again... Return to the start of the loop
8        continue
9    else:
10        #age was successfully parsed!
11        #we're ready to exit the loop.
12        break
13if age >= 18: 
14    print("You are able to vote in the United States!")
15else:
16    print("You are not able to vote in the United States.")
17