269. Python Program to Check a Sign-Up Form and Show Every Error
HardPhase 3 · Challenge
There are many rules here, but each one is a small check with tools you know: string methods, slicing, a list and if. Try Example 2 on paper first.
Overview
Good sign-up forms show every problem at once, so you can fix them all in one go. This program is the check that runs when you press the sign-up button.
Problem Statement
Ayesha is building the sign-up page for a club website. The form has 4 fields: a username, an email, an age and a password. Read them, one per line, in that order.
Check the 11 rules below, from rule 1 to rule 11. For each rule that fails, print its message. So the messages always come out in rule order.
| Rule | Message |
|---|---|
| 1. Username: 3 to 12 characters | Username: must be 3 to 12 characters |
2. Username: only letters, digits and _ | Username: use only letters, digits and _ |
| 3. Username: starts with a letter | Username: must start with a letter |
| 4. Email: a simple email shape (see below) | Email: must look like name@site.com |
| 5. Age: only digits | Age: must be a whole number |
| 6. Age: from 13 to 120 | Age: must be from 13 to 120 |
| 7. Password: 8 characters or more | Password: must be at least 8 characters |
| 8. Password: has a digit | Password: must have a digit |
| 9. Password: has an uppercase (capital) letter | Password: must have an uppercase letter |
| 10. Password: has a lowercase (small) letter | Password: must have a lowercase letter |
| 11. Password: does not contain the username (capital letters do not matter) | Password: must not contain the username |
The email has a simple email shape when all of these are true:
- It has exactly one
@. - There is at least one character before the
@. - The part after the
@has a dot, and it does not start or end with a dot.
Check rule 6 only when rule 5 passes. If the age is not a whole number, print only the rule 5 message for it.
If no rule fails, print Sign-up OK.
Examples
Example 1. Input:
mei_88
mei@mail.com
25
Sunny2day
gives:
Sign-up OK
Why: the username has 6 allowed characters and starts with a letter. The email has one @ and a dot after it. 25 is from 13 to 120. The password has 9 characters, a digit, an uppercase and a lowercase letter, and no mei_88 in it.
Example 2. Input:
9lives
lives@site
abc
lives9LIVES
gives:
Username: must start with a letter
Email: must look like name@site.com
Age: must be a whole number
Password: must not contain the username
Why: the username starts with a digit. The part after the @ has no dot. abc is not a number, so rule 6 is not checked. The password lives9LIVES contains 9LIVES, which is the username with capital letters.
Input
Four lines: the username, the email, the age and the password. Each line has 1 to 30 characters and no spaces. Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.
Output
Every error message, one per line, in rule order, or the single line Sign-up OK. The tests try 9 different inputs, also a form that breaks 7 rules, the ages 12, 13, 120 and 121, an age of -5, and an email with two @ signs.
Try it first. The solution opens when you solve this question, or after 3 tries with Submit.