- Read the question carefully (twice).
- Break the task into the smallest steps.
- Sketch or write pseudocode before coding.
- Start small — test as you go.
- Check your solution with different cases.
Problem Description
Email addresses usually follow a specific pattern:
- They have a username (letters, numbers, and some symbols like
_or.). - They contain an
<strong>@</strong>symbol. - They have a domain name (like
gmail.comorschool.edu). - The domain usually contains a dot (
<strong>.</strong>) followed by at least two letters (like.com,.org,.net).
Your task is to write a program that checks if a string looks like a valid email address.
Learning Goals
By completing this problem, you should be able to:
- Work with strings in your chosen programming language.
- Use basic string operations such as
find,in,split, or slicing. - Apply if/else logic to check conditions.
- Understand how to test your code with different inputs.
Tasks
Task 1: Basic Check for @
Write a program that asks the user to enter a string.
- Check if the string contains the
@character. - If it does, print
"This looks like an email." - Otherwise, print
"This is not a valid email."
Task 2: Check for @ and .
Update your program:
- The string must contain both
@and.to be considered valid. - If both are present, print
"Valid email format." - Otherwise, print
"Invalid email format."
Task 3: Username and Domain Split
Modify your program to:
- Split the string into two parts: before the
<strong>@</strong>and after the<strong>@</strong>. - Check that:
- The username (before
@) is not empty. - The domain (after
@) contains a..
- The username (before
- Print
"Valid email"only if both conditions are true.
Task 4: Test Cases
Test your program with the following inputs. Write down what your program prints for each one:
[email protected]notanemail@missingusername.comusername@domain[email protected]
Extension Challenge (Optional)
Improve your program so that it:
- Only allows letters, numbers, dots (
.), dashes (-), and underscores (_) before the@. - Makes sure the domain ends with
.com,.org, or.edu.
✅ Success Criteria:
- You can correctly identify whether a string looks like a valid email.
- You can explain how your code works step by step.
- Your program works with the test cases provided.