Browser lab15 min+60 XP

Validating input

Users type the wrong things. Check input before it crashes your program.

0% done

Code runs right here in your browser. Nothing to install.

TASK 1

Learn it

Key idea

Check input with methods like isdigit() before converting it.

Key words
age = input("Age: ")age.isdigit() ?int(age) is safecarry onPlease type awhole numberTrueFalseask again
The program keeps asking until the input is a whole number.

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

Real life linkA sign-up form that says "password must be at least 8 characters" is validating input. It checks before it accepts.
Example code
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))
TASK 2

Try it yourself

+10 XP

Keep 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.

TASK 3

Capture the flag

+10 XP

What 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.

HOMEWORK

Password strength checker

+20 XP

Write 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.