Validating input
Users type the wrong things. Check input before it crashes your program.
Code runs right here in your browser. Nothing to install.
Learn it
Check input with methods like isdigit() before converting it.
String test methods return True or False: isdigit() (only digits), isalpha() (only letters), isalnum() (letters or digits), isupper(), islower() and isspace().
Use them to check input before converting it: if answer.isdigit() is True, int(answer) is safe. Combine with a while loop to keep asking until the input is valid.
To check a password has at least one digit, loop through its characters, or use any(ch.isdigit() for ch in password).
for test in ["42", "4.2", "abc", "abc42", " "]: print(repr(test), test.isdigit(), test.isalpha(), test.isalnum()) pw = "Secret9" print(any(ch.isdigit() for ch in pw), any(ch.isupper() for ch in pw))
Try it yourself
+10 XPKeep asking Age: until the user enters only digits. Then print Thanks! You are 14.
Create a free account to start this lab
Sign in to write, run and check your code here. Your progress, XP and code are saved to your account. No card needed.
Capture the flag
+10 XPWhat does "abc123".isalpha() return?
Create a free account to start this lab
Sign in to answer the flag question. Your progress, XP and code are saved to your account. No card needed.
Password strength checker
+20 XPWrite check_password(pw) that returns a list of problems: shorter than 8 characters, no digit, no capital letter, no lowercase letter. Test it with "vault" and "Vault2026". Print each problem, or Strong password if there are none.
Create a free account to start this lab
Sign in to do this homework and have it checked. Your progress, XP and code are saved to your account. No card needed.