Asking the User for Input Until They Give a Valid Response

Continue to ask for input until correct answer is given(Python) (Non number values)

break is what you want. Use it like so:

while 1:

answer = input("What is your favorite eldest daughter's name? ").lower()

if answer == "a" or answer == "al": #assuming this is the right answer
print("Of course, we all knew that")
break
elif answer == "c" or answer == "chris":
print("""Oh she must be standing right there.
But that\'s not it, try again.""")

elif answer == "e" or answer == "ed":
print("Not even the right gender. Try again.")
elif answer == "n" or answer == "nic":
print("Only biological children please.")

else:
print("I'm sorry. Only children. Were you thinking of the dogs?")

How to run python loop until correct input string is entered

Change beginning of code to:

while True:
name_question = input("You are the President of RSA, what is your name?: ")
if name_question == "Cyril":
name = True
break
else:
print("\nThat is incorrect, please try again.\n")

Take some inputs until 0 encountered, print each word in lowercase and break the loop

I'd suggest this, it's a combination of these 2 comments, sorry

count = 0
while True:
a = input()
if a == '0':
break
else:
print(a.lower())
count += 1
print(count)


Related Topics



Leave a reply



Submit