Create a program that checks for syntactical errors in an email address.

#Of course this question could be expanded into a program as large or small as desired, dependant on how rigorous wants the program to become. The setup I created here checks for some basic syntactical errors, such as there being too many or too few @ symbols, ensuring that there is at least one dot after the @ symbol, and that such a dot is not the final character in the address. #Ask user for email to check emailAddress = str(raw_input("Enter the email address that needs checking. >>>")) #Establish the boolean variable for a valid or invalid email and the error message if it is incorrect emailValid = True errorMessages = [] #Check for spaces and one @ symbol by cycling through the characters in the email address given. Records location of @ symbol for later noOfSpaces = 0 noOfAtSymbs = 0 for i in emailAddress:  if i == " ":    noOfSpaces += 1  elif i == "@":    noOfAtSymbs += 1    atIndex = emailAddress.index("@") if noOfSpaces != 0:  emailValid = False  errorMessages.append("Email addresses cannot contain spaces. ") if noOfAtSymbs != 1:  emailValid = False  errorMessages.append("Email addresses must contain a single '@' symbol. ") #Checks to see if there is at least one . after the @ try:  if "." not in emailAddress[atIndex:]:    emailValid = False    errorMessages.append("Email address must contain at least one '.' after the @ symbol") except NameError:  pass #Ensures that . is not final character if emailAddress[-1] == ".":  emailValid = False  errorMessages.append("Email address cannot end in '.'") #If no erroes found, return validation. If not, return why the address is invalid if emailValid == True:  print("This email address is valid.") elif emailValid == False:  print("Invalid email address:")  for i in errorMessages:    print i

Answered by Christopher H. Python tutor

1529 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

Write a python function that takes a string as parameter and returns the character in the string with the most occurrences, along with the number of times this character occurs


What built-in types does python provide?


How do I use for loops in Python?


Firstly, my question is not Python related, but maths A-level related. I don't know how to change that in my profile but I would like to only tutor maths GCSE and potentially A-level. My question: Integrate sin(x)^3 over x=0 and x=pi/2.


We're here to help

contact us iconContact usWhatsapp logoMessage us on Whatsapptelephone icon+44 (0) 203 773 6020
Facebook logoInstagram logoLinkedIn logo
Cookie Preferences