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 :: Print a nested list line by line in python 
Python :: how to add card using py-trello API 
Python :: how to make all time greeter using python 
Python :: Change the year in 2nd line to get the answer for the year you want. Ex: year=2010 
Python :: word pattern in python 
Python :: how to check if its later than python 
Python :: how to clear screen python 
Python :: python round up 
Python :: filter function using lambda in python 
Python :: python scatterplot 
Python :: how to get the amount of nan values in a data fram 
Python :: pandas replace nulls with zeros 
Python :: saving to csv without the index 
Python :: how to convert list into string in python 
Python :: install chromedriver ubuntu python 
Python :: discord.py on command error 
Python :: remover espaços string python 
Python :: python google search results 
Python :: resize numpy array image 
Python :: seconds in a month 
Python :: convert 2d list to 1d python 
Python :: cmd python -m 
Python :: Scrape the text of all paragraph in python 
Python :: start new app in django 
Python :: how to print alternate numbers in python 
Python :: flask api response code 
Python :: keras read image 
Python :: python invert dictionary 
Python :: django expressionwrapper example 
Python :: update python in miniconda 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =