CurriculumPython input() and Type Conversion for KidsValidating input
Validating Python input() with a while Loop
Re-ask until the answer is actually valid — using a while loop wrapped around input().
Validating input
So far, every level trusted the player's FIRST answer. Real programs can't always do that.
You already know while loops repeat until a condition changes. Wrap one around input(), and you can keep asking until the answer is actually good.
while True:
answer = input("Password? ")
if answer == "dragon":
break
print("Nope, try again.")
print("The gate opens!")Output
Nope, try again.
The gate opens!while True: means 'repeat forever' — the ONLY way out is the break inside the if. That's the whole pattern: ask, check, break if good, otherwise loop back and ask again.
while True:
answer = input("Password? ")
if answer == "dragon":
break
print("Nope, try again.")This works for numbers too — not just wrong answers, but numbers that convert fine but break a GAME rule, like gold that shouldn't be negative.
int('-5') converts perfectly well — it's a real, valid number. The while loop re-asks because -5 fails YOUR rule (gold > 0), not because Python refused to convert it.
while True:
gold = int(input("Gold? "))
if gold > 0:
break
print("Try again.")But some text can't be converted AT ALL — and that's a different, real crash.
int('abc') raises a ValueError. Not the TypeError from before — this is Python saying 'abc' was never digits, so there's nothing to convert.
int('abc')Output
ValueError: invalid literal for int() with base 10: 'abc'answer.isdigit() checks whether text is made ENTIRELY of digits, without converting it — True or False, and it never crashes, even on 'abc'.
"abc".isdigit()Output
FalsePut it together: check .isdigit() BEFORE calling int(), inside a while loop. Bad text gets re-asked instead of ever reaching int() at all.
while True:
age_text = input("Age? ")
if age_text.isdigit():
age = int(age_text)
break
print("Not a whole number — try again.")Check your understanding
A level re-asks "Password? " until the player types "dragon", using while True: with a break inside an if. The player types "wrong" first, then "dragon". How many times does the loop ask?
while True:
answer = input("Password? ")
if answer == "dragon":
break
print("Nope, try again.")Why: Twice. The first answer (wrong) fails the if, so the loop asks again; the second answer (dragon) passes the if and break ends the loop. while True: plus a break is exactly how you repeat 'ask again' an unknown number of times, however many wrong guesses it takes.
gold = int(input("Gold? ")) and the player types "-5". Does this crash?
gold = int(input("Gold? "))Why: No crash — int('-5') is a completely valid conversion. Rejecting -5 as bad gold is a rule YOUR code has to check with an if, same as any other re-ask loop, not something int() does automatically.
The player types "abc" where the code does int(input("Age? ")). What happens?
age = int(input("Age? "))Why: A ValueError — Python's honest way of saying 'abc' was never digits, so int() has nothing valid to convert. Checking answer.isdigit() BEFORE calling int() is how you avoid ever triggering it.
What you'll practice
The dragon's gate only opens for the password "dragon". Keep asking "Password? " until the answer is exactly "dragon" — print "Nope, try again." for every wrong guess, then once the right password arrives, break out and print "The gate opens!". The player will answer wrong once, then right.
More from this topic
Validating input is one lesson inside Python input() and Type Conversion for Kids — see the full lesson order and what the whole topic covers.
View the full topic →