1

I'm working on a script that allows users to input an email. If the email has a 'username' and 'domain' such as username@domain.com, then the program should output that the email is an email. If the user enters something such as username@, then the program should output that the input is not a valid email address.

The function should return True when the text is an email, otherwise it should return False.

I'm having a problem getting the program to return False when entering an address like 'username@' or '@gmail.com'.

I currently have the following code:

text = ''
tokens = 0
split_counter = 0

def isEmail(text):
    tokens = text.split('@')
    split_counter = len(tokens)
    if split_counter == 2:
        print(text, '==> EMAIL')
    else:
        print(text, '==> NOT EMAIL')
    return

while True:
    text = input("Email: ")    
    if text == 'quit':
        print('Later!')
        break
    else:
        isEmail(text)

Thank you.

Alfred
  • 25
  • 5
  • You have described what you're trying to do but haven't described what the issue you're facing is. – Dimitris Fasarakis Hilliard Mar 09 '17 at 08:25
  • 2
    Possible duplicate of [Python check for valid email address?](http://stackoverflow.com/questions/8022530/python-check-for-valid-email-address) – Jean-François Fabre Mar 09 '17 at 08:25
  • Sorry, updated it. I can't seem to get a False return when entering something like '@gmail.com' or 'username@'. To do that I think I would have to add something after the split. – Alfred Mar 09 '17 at 08:42

5 Answers5

0

I would recommend using regex and looking at the other answers on StackOverflow for checking for a valid email. But in response to your question, from what you commented above, it is not returning false when you try "@gmail.com". This is because when you split a string by "@", it returns

string = "@gmail.com"
string.split("@")
>>> ['', 'gmail.com']

Therefore, the length of that returned list will be 2. You could make another check inside the split_counter list:

if split_counter == 2 and tokens[0] != '':
    print(text, '==> EMAIL')

I would recommend printing whatever you have along the way as a way of debugging your code. It'll help you avoid errors like this in the future. You knew that the condition had to be wrong, and the condition is only dependent on split_counter, so you could have printed split_counter to see what went wrong.

  • I'm working on a lab for class and we haven't used the regex yet, so I didn't want to use the other examples I found. I understand what split does, I just thought there might be a way to print NOT EMAIL if one of the values in the split list was under 3 characters or something of that nature. – Alfred Mar 09 '17 at 08:56
  • Alright! Did adding the check for split_counter[0] != '' fix your problem? – Abhishek Tumuluru Mar 09 '17 at 08:57
  • It did not. Now I'm getting 'TypeError: 'int' object is not subscriptable' when I try and enter something into the input box. – Alfred Mar 09 '17 at 08:59
  • I misread the question. it should be tokens[0] != ''. Add a check for tokens[1] != '' also. Split counter is an integer, so it cannot be indexed. This should work. – Abhishek Tumuluru Mar 09 '17 at 09:04
0

The split method always return two strings if there is only one @. For example:

>>> text="foo@"
>>> text.split('@')
['foo', '']

So you should check that the two returned strings have a length greater than zero.

GLR
  • 1,070
  • 1
  • 11
  • 29
0

You can see the problem is split() function:

>>> text = "@gmail.com"
>>> tokens = text.split("@")
>>> tokens
['', 'gmail.com']

Best practice is to use regex to check valid emails instead of writing hard code:

def isEmail(text):
    if len(text) > 6:
        if re.match(r'\b[\w.-]+@[\w.-]+.\w{2,4}\b', text) != None:
            # do something
    else:
        # do something

If you don't know what is regex, then this is probably the right time to learn it.

Huy Vo
  • 2,418
  • 6
  • 22
  • 43
0

A simple solution could be to leverage all() and the fact, that bool("") is False for that:

>>> email = "@gmail.com"
>>> s = email.split("@")
>>> all(s)
>>> False

>>> email = "a@"
>>> s = email.split("@")
>>> all(s)
>>> False

>>> email = "a@gmail.com"
>>> s = email.split("@")
>>> all(s)
>>> True

But beware, that this code doesn't say anything about the "correctness" of an email adress. It only says something about whether a string could be split by @ and none of the parts is empty.

For that purpose I would rely on validators provided by frameworks like Django or something like WTForms.

Thomas Junk
  • 5,588
  • 2
  • 30
  • 43
0

All you're missing is another check on line 11:

if split_counter == 2 and len(tokens[0]) > 0 and len(tokens[1]) > 0:
    print(text, '==> EMAIL')
  • Worked like a charm. I just changed the 0 in 'len(tokens[1]) > 0:' to 2 so it requires a different length. Thank you very much! – Alfred Mar 09 '17 at 09:15