Asking the user to input a proper email address 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) &lt;= 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) &lt;= 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: ")
Okay so as a general comment, validation like this is fine (well except
it's discarding potentially valid addresses with the check for . and
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. 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) &lt;= min_length:
            email_address = input("Your email address is too short\nPlease write your email address again: ")
            continue
        return email_address


The loops in there are doing a bit too much, I'll leave that as an
exercise for the reader to optimise it.