Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

reject invalid input using a loop in python

take input
while incorrect input:
    take input
    
#Eg. Taking the month input for the first quarter of the year.
months = ['january', 'february', 'march']
month = input('Select the month').lower()
while month not in months:
  month = input('Oops! Incorrect input. Select month again').lower()
  

  
Comment

python loop invalid input

while True:
    try:
        # Note: Python 2.x users should use raw_input, the equivalent of 3.x's input
        age = int(input("Please enter your age: "))
    except ValueError:
        print("Sorry, I didn't understand that.")
        #better try again... Return to the start of the loop
        continue
    else:
        #age was successfully parsed!
        #we're ready to exit the loop.
        break
if age >= 18: 
    print("You are able to vote in the United States!")
else:
    print("You are not able to vote in the United States.")
Comment

PREVIOUS NEXT
Code Example
Python :: how to use google translate api in python 
Python :: pandas converters example 
Python :: main.py : invalid syntax 
Python :: How to test if a webpage is an image python requests 
Python :: how to check for non-datetime value in python 
Python :: Install pip and add virtual environment to the Python Kernel 
Python :: primary neural network 
Python :: pandas convert text duration to minutes 
Python :: how to remove no data times plotly 
Python :: python print string 
Python :: how to take multiple input python 
Python :: seeparate string without split function python 
Python :: python merge two byte files 
Python :: Multiple sub in single regex. 
Python :: jupyter notebook print string with variables 
Python :: pandas parameters read 
Python :: sns regplot make the line and confidence interval thicker 
Python :: torch.tensor.expand 
Python :: the code panda 
Python :: pvector python processing 
Python :: python redirect console output to devnull 
Python :: function to sort a list of points based on their x and y-coordinates 
Python :: django froms 
Python :: temp python 
Python :: re.split 
Python :: how to make dice roll in python 
Python :: convert dictionary to string 
Python :: global variable in python 
Python :: how to make a screen in pygame 
Python :: python schema 
ADD CONTENT
Topic
Content
Source link
Name
6+6 =