Problem Set 7: email validator

When you start a new problem set, your first instinct might be to open your computer and begin typing code right away. While this can feel productive, it often leads to frustration when things don't work as expected. Instead, take a few minutes to slow down and plan.

Here are some helpful strategies:

  1. Understand the problem clearly
    • Read the instructions carefully — twice if needed.
    • Ask yourself: What exactly is being asked?
  2. Break the problem into smaller steps
    • Think about the smallest possible actions the computer will need to perform.
    • For example: If the task is to find the first recurring letter in a word, what steps must happen first?
  3. Try solving it on paper first
    • Write out your steps in plain language (pseudocode).
    • Test your steps with a simple example before touching the keyboard.
  4. Translate your steps into code
    • Start small — write only a few lines at a time and test often.
    • Don't worry about perfection at first; get a working version, then improve it.
  5. Check your solution
    • Run it with different examples, including edge cases.
    • Ask: Does this solve the problem in all situations?

  • 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.com or school.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 ..
  • 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:

  1. [email protected]
  2. notanemail
  3. @missingusername.com
  4. username@domain
  5. [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.