Email validator
This is a problem set. Some of these are easy, others are far more difficult. The purpose of these problems sets are to HELP YOU THINK THROUGH problems. The solution is at the bottom of this page, but please don't look at it until you have tried (and failed) at least three or four times.
What is this problem set trying to do[edit]
You are going to use very simple if not in logic here. If you complete this problem set, you will have shown me you sort of understand how to test a string for a specific character. You will have ALSO shown me that you know how to program a very simple validator.
The Problem[edit]
You should write a program that asks a user to enter an email address. Your program should check if the email:
- has an @ sign
- has a period .
- has a common top level domain (org, com)
Take This Further[edit]
- you can test for a @ and a . Could you also test for common misspellings?
- if you can test for common misspellings, could you suggest the correct spelling?
- we can check for common .com, .org, addresses but what about other top level domain names click here for a fairly scary list of them
How you will be assessed[edit]
Every problem set is a formative assignment. Please click here to see how you will be graded
References[edit]
A possible solution[edit]
Click the expand link to see one possible solution, but NOT before you have tried and failed at least three times
top_level_domains = ['com', 'org', 'pl', 'kz', 'edu']
while True:
email = raw_input("Enter you email ")
if '@' not in email:
print("Error: you need an @ sign in your email.")
break
elif '.' not in email:
print("Error: you need a . sign in your email. Please try again")
break
# I am grateful to my student Sneha for this line below. It is a succinct way of capturing the characters AFTER the period.
elif email[email.index('.')+1:] not in top_level_domains:
print("Error: you need a valid domain name at the end")
else:
print("Success: the email address you entered has passed our basic validation tests.")
break