patternpythonMinor
Asking the user to input a proper email address
Viewed 0 times
propertheaddressemailuserinputasking
Problem
The purpose of this code is to ask the user to input an email address but it must contain "@" and ".co". The code works but it is too long so I would like to know if there is any way to shorten it using functions.
email_address = input("What is your email address? ")
while "@" not in email_address:
email_address = input("Your email address must have '@' in it\nPlease write your email address again: ")
if len(email_address) <= 6 :
email_address = input("Your email address is too short\nPlease write your email address again: ")
if "." not in email_address:
email_address = input("Your email address must have '.' in it\nPlease write your email address again: ")
while "." not in email_address:
email_address = input("Your email address must have '.' in it\nPlease write your email address again: ")
if len(email_address) <= 6 :
email_address = input("Your email address is too short\nPlease write your email address again: ")
if "@" not in email_address:
email_address = input("Your email address must have '@' in it\nPlease write your email address again: ")Solution
Okay so as a general comment, validation like this is fine (well except
it's discarding potentially valid addresses with the check for
the minimum length, but I guess you know that), but in a real
application you most likely want to just send an email and check if it
was received, that also deals with a whole lot of other problems.
That said, of course this snippet can be shortened. I don't see why the
two permutations of the loop are necessary in the first place.
Everything duplicated has the potential to be eliminated, but the
result might be more or less readable and possibly longer (because there
are only two cases here for example, so making the code more generic
would only pay off with more cases).
What's the function going to be here? Checking if all the characters
are in the email string, e.g.
Only the specific characters really. So pass that in and return the
final address (without using globals) to be future proof.
Looks like this:
Lastly, the check function is very simple too:
The loops in there are doing a bit too much, I'll leave that as an
exercise for the reader to optimise it.
it's discarding potentially valid addresses with the check for
. andthe minimum length, but I guess you know that), but in a real
application you most likely want to just send an email and check if it
was received, that also deals with a whole lot of other problems.
That said, of course this snippet can be shortened. I don't see why the
two permutations of the loop are necessary in the first place.
Everything duplicated has the potential to be eliminated, but the
result might be more or less readable and possibly longer (because there
are only two cases here for example, so making the code more generic
would only pay off with more cases).
What's the function going to be here? Checking if all the characters
are in the email string, e.g.
check_email_contains. What varies?Only the specific characters really. So pass that in and return the
final address (without using globals) to be future proof.
Looks like this:
email_address = check_email_contains(input("What is your email address? "), "@.")Lastly, the check function is very simple too:
def check_email_contains(email_address, characters, min_length=6):
while True:
for character in characters:
if character not in email_address:
email_address = input("Your email address must have '{}' in it\nPlease write your email address again: ".format(character))
continue
if len(email_address) <= min_length:
email_address = input("Your email address is too short\nPlease write your email address again: ")
continue
return email_addressThe loops in there are doing a bit too much, I'll leave that as an
exercise for the reader to optimise it.
Code Snippets
email_address = check_email_contains(input("What is your email address? "), "@.")def check_email_contains(email_address, characters, min_length=6):
while True:
for character in characters:
if character not in email_address:
email_address = input("Your email address must have '{}' in it\nPlease write your email address again: ".format(character))
continue
if len(email_address) <= min_length:
email_address = input("Your email address is too short\nPlease write your email address again: ")
continue
return email_addressContext
StackExchange Code Review Q#153079, answer score: 4
Revisions (0)
No revisions yet.