“Python Loop Nieprawidłowe wejście” Kod odpowiedzi

Odrzuć nieprawidłowe wejście za pomocą pętli w Pythonie

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()
  

  
Kwams

Python Loop Nieprawidłowe wejście

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.")
Motionless Manatee

Odpowiedzi podobne do “Python Loop Nieprawidłowe wejście”

Pytania podobne do “Python Loop Nieprawidłowe wejście”

Więcej pokrewnych odpowiedzi na “Python Loop Nieprawidłowe wejście” w Python

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu