0

I need to identify if the string is at least 2 characters or else, they need to input the string again. The code doesn't work, what did I do wrong?

For example, I type "jason@yahoo.c":

email_address = input('Enter your email address: ')

dns_label = email_address.split('.')
local = dns_label[0]
dns = dns_label[1]

if not len(local)>=2 and len(dns)>=2:
    email_address = input('''Your email address is incorrect.\n Please type your email address again: ''')
else:
    print(email_address,'is valid')

After the dot it only has 1 character, so it will ask the user again for the input. What did I do wrong?

Selcuk
  • 57,004
  • 12
  • 102
  • 110
Shun Lushiko
  • 23
  • 1
  • 9

1 Answers1

2

The not operator has higher precedence over and, therefore you must put the expression into brackets:

if not (len(local) >= 2 and len(dns) >= 2):

or better, remove the not altogether to make it more readable:

if len(local) < 2 or len(dns) < 2:

Edit: That being said, an if clause is not sufficient for input validation as it will only check the entry once. What if the user types an incorrect email address the second time too? See this question for a detailed explanation.

Selcuk
  • 57,004
  • 12
  • 102
  • 110