Guard Clauses: Validate User Registration

Medium ⏱ 12 min 59% acceptance ★★★★☆ 4.4
Write a function validate_registration(username, age, email) that returns the first applicable error message using guard clauses, or "OK" if everything is valid. Rules, checked in order: if len(username) < 3, return "Username too short". If age < 13, return "Too young to register". If "@" not in email, return "Invalid email". Otherwise return "OK".

Examples

Example 1
Input
validate_registration("ab", 20, "a@b.com")
Output
"Username too short"
Explanation

Username fails the first guard clause.

Example 2
Input
validate_registration("alice", 10, "a@b.com")
Output
"Too young to register"
Explanation

Username passes; age fails.

Example 3
Input
validate_registration("alice", 20, "no-at-sign")
Output
"Invalid email"
Explanation

Only the email check fails.

Example 4
Input
validate_registration("alice", 20, "a@b.com")
Output
"OK"
Explanation

All checks pass.

Constraints

  • username and email are strings.
  • age is an integer.

Topics

Conditional Statementsguard clauses

Companies

AirbnbUber

Hints

Hint 1

Return early from each guard clause instead of nesting deeper.

Hint 2

The order of the checks matters: earlier failures should short-circuit the rest.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.