Last additions
Lesson 7 of 8 in Coddy's Email validator using Python course.
To wrap it all, let's add clear messages to the user.
Challenge
EasyAdd the following support to your program:
- instead of printing
TrueorFalse, print"Email is valid"or"Email is invalid"depend on the case. - before the user get asked for input, print the message "
Enter email:", with new line.
Try it yourself
PERMITS_RECIEPENT = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._'
PERMITS_DOMAIN = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-'
def validate(email):
if not email.count('@') == 1:
return False
recipient_name = email.split('@')[0]
if len(recipient_name) > 24 or len(recipient_name) < 3:
return False
for c in recipient_name:
if c not in PERMITS_RECIEPENT:
return False
if '.' == recipient_name[0] or '.' == recipient_name[-1] or '_' == recipient_name[0] or '_' == recipient_name[-1]:
return False
domain_name = email.split('@')[1].split('.')[0]
if len(domain_name) > 12 or len(domain_name) < 3:
return False
for c in domain_name:
if c not in PERMITS_DOMAIN:
return False
top_level_domain_name = email.split('@')[1].split('.')[1]
if top_level_domain_name not in ['com', 'org', 'net', 'tech']:
return False
return True
if __name__ == "__main__":
email = input()
is_valid = validate(email)
print(is_valid)