-1
import string
# Imported string to load dictionaries

d = dict.fromkeys(string.ascii_letters, 0)
ds = dict.fromkeys(string.digits, 0)
# 2 dictionaries imported, letters and numbers(digits)

f = {**d, **ds, ".":0}
# Merged both dictionaries together as well as "." as there was no specific 
dictionary.

while True:
   `email = input("Enter the name of the file (must be .txt file)")
    file = open(str(email), "r")
    for line in file:
        for i in line:
            if " " in line:
                print(line, "Space found")
                break

        if "@" not in line:
            print(line, "The email needs @")

        emailzero = line.split("@")
        if len(emailzero) < 2:
            print(line, "@ and a domain symbol is needed")
            continue

        if emailzero[0] == "":
            print(line, "You must enter a name")

        if emailzero[1] == "":
            print(line, "A Domain is needed")

        if "." in emailzero[0]:
            print(line, "Only alphabet and numbers")

        for i in emailzero[0]:
            if i not in f:
                print(line, "The email must have a valid character before @")
                break

        if "." not in emailzero[1]:
            print(line, "The domain needs to contain a .")

        for i in emailzero[1]:
            if i not in f:
                print(line, "The email must not contain any invalid characters in the domain.")
                break

So here's what my program does. It takes in a file of emails and checks it one by one to see if it is valid.

The problem is when I load a list with more than one email, the emails after the first all end up saying "The email must not contain any invalid characters in the domain."

Can someone tell me why this happens and how I can fix it?

Cheers,

poisonishere
  • 43
  • 1
  • 7
  • Please fix the indentation of your code. As it stands, your code is not valid Python because the indentation is wrong. (There is a `while` loop and two `for` loops where the next line is not indented further.) If your problem relates to indentation, and the code you've posted above doesn't contain the same indentation as your actual code, we will struggle to help you. – Luke Woodward Oct 07 '17 at 20:14
  • Add print segments to your code for easier debugging. Your for loop/indentation looks wrong. Don't you mean for each line in the file check for all those things that you are checking? Right now it's not doing that. – D.Zou Oct 07 '17 at 20:33
  • As an aside, even if this code was working correctly it will still give incorrect results since some conditions are too narrow and others are missing. – Ignacio Vazquez-Abrams Oct 07 '17 at 20:36
  • @poisonishere Post code that shows your issue. The code you posted raises `IndentationError`. – Stop harming Monica Oct 07 '17 at 20:51
  • @poisonishere Put some effort in debugging. Just `print(repr(line))` will show you the answer. – Stop harming Monica Oct 07 '17 at 20:55
  • @poisonishere There is no indent after `if " " in line:`, that's a syntax error and the code does not run. – Stop harming Monica Oct 07 '17 at 20:56

2 Answers2

0

Starting with the line if "@" not in line:, you need to give everything another indentation, I think.

jcfollower
  • 3,103
  • 19
  • 25
0

When reading from a file, your lines will end with a newline character of some sort (\r\n or \n). If you want to parse it line by line, you should make sure to strip the line of trailing and leading spaces:

emailzero = line.strip().split("@")

See string.strip

Additional tips:

  1. Regarding this snippet:

Snippet:

d = dict.fromkeys(string.ascii_letters, 0)
ds = dict.fromkeys(string.digits, 0)
# 2 dictionaries imported, letters and numbers(digits)

f = {**d, **ds, ".":0}

You could condense all this into a set, like this:

allowed_domain_chars = set(string.ascii_letters) | set(string.digits) | { '.' }
  1. At the cost of having less readable error messages, you could condense this into a regular expression.

  2. Your parsing does not accept all valid email addresses (such as those that include a + or a . in the left side of the @). Something like email_regex = re.compile(r'.+@.+\..+') would be a simple regex that covers most use cases (given the issues with validating email).

Casey Kuball
  • 7,717
  • 5
  • 38
  • 70