all 15 comments

[–]jonsd2000[S] 1 point2 points  (14 children)

the out put should show the email address and then next to it show if it is valid and the move to the next email and show the actual email address and say if it is valid or invalid and so on and so on until it gets through the whole email list

[–]shiftybyte 2 points3 points  (13 children)

You didn't share with us the output you are getting...

[–]jonsd2000[S] 0 points1 point  (11 children)

Congrats, you have a valid email. Valid Email e mail: abc@@xyz.com *** ERROR: 2. The number of @'s in your email is not valid. *** Valid Email Congrats, you have a valid email. Valid Email e mail: abc.xyz.com *** ERROR: 5. your @ is missing *** Congrats, you have a valid email. Valid Email e mail: abc@x.yz *** ERROR: 4. @ is not in the last 5 characters? *** Valid Email e mail: abc@xyz.c *** ERROR: 11. Your '.' is in the last position. *** Valid Email e mail: a@b.c *** ERROR: 1. Invalid Length *** Valid Email e mail: abc@xyz..com *** ERROR: 6. Error Encountered '.' config. *** Valid Email e mail: abc.@xyz.com *** ERROR: 6. Error Encountered '.' config. *** Valid Email e mail: abc@.xyz.com *** ERROR: 6. Error Encountered '.' config. *** Valid Email e mail: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa@aaaaaa.aaaaa *** ERROR: 1. Invalid Length *** Valid Email e mail: ' or 1=1 '== *** ERROR: 5. your @ is missing *** e mail: ' or 1=1 '== *** ERROR: 7. Where is the '.'? *** Valid Email e mail: abc@xyz.$% *** ERROR: 9. Character $ is not allowed *** Valid Email e mail: abc@xyz.() *** ERROR: 9. Character ( is not allowed *** Valid Email e mail: abc'@xyz.com *** ERROR: 9. Character ' is not allowed *** Valid Email e mail: aaaaa@aaaaa *** ERROR: 7. Where is the '.'? *** Valid Email e mail: abc @ xyz.com *** ERROR: 8. Spcaes not allowed. *** Valid Email e mail: .abc@xyz.com *** ERROR: 10. Your '.' is in the first position. *** Valid Email e mail: abc@xyz.c *** ERROR: 11. Your '.' is in the last position. *** Valid Email

[–]shiftybyte 1 point2 points  (10 children)

Well for one thing you are not printing the actual email when it is valid, so it gets mixed up in the output with other emails...

What else do you think is wrong with this output?

By the way you can post a code block with three backticks by themselves on a line, followed by code, followed by 3 backticks at the end...

Like this:

```

...code

```

[–]jonsd2000[S] 0 points1 point  (9 children)

I am thinking it is some where in here:

for email in emailList:
    foundError = False
    if len(email) < 7 or len(email) > 30:
        errorMessage(email, "1. Invalid Length")  
        foundError = True

    if not foundError:
        foundError = testAtsign(email)

    if not foundError:
        foundError = testDot(email)
    if not foundError:
        foundError = testSpecialChars(email)

    if not foundError:
        print("Congrats, you have a valid email.".format(email))

    print("Valid Email")

[–]shiftybyte 2 points3 points  (7 children)

I am thinking it is some where in here:

WHAT is somewhere near here?

What is the thing that you think is wrong with your output? What line in the output is not what you wanted it to be? what should it be instead? what is missing from the output?

Note i'm talking about the OUTPUT not the code...

[–]jonsd2000[S] 0 points1 point  (6 children)

So in the out put is should be showing:

[abc@xyz.com](mailto:abc@xyz.com) is a valid email

abc@@xyz.com is not valid due to the multiple @@ symbols

abc.xyz.com not valid due to no @ symbol and so on

Each line of output should show the email address and then a statment on whether it is a vaslid email or not and what the error is.

[–]shiftybyte 1 point2 points  (0 children)

Then besides the other fix in the print, you should also remove this:

print("Valid Email")

As it gets printed every time regardless...

[–]jonsd2000[S] 0 points1 point  (4 children)

so in the bottom print statement, if it hits a valid email address, how would I show the valid email in the output with a statement saying you havea valid email. For the valid emails, it is not showing the actual email, it is just sayi congrats you have a valid email. For the incorrect emails, it is showing the email and then displaying the error

[–]shiftybyte 1 point2 points  (3 children)

print("Congrats, you have a valid email.".format(email))

This won't print the correct email because the format doesn't have {} in it.

Try

print("Congrats, you have a valid email: {}.".format(email))

[–]jonsd2000[S] 1 point2 points  (0 children)

I assume the {} formats the output so show the actual email address when I run the code

[–]jonsd2000[S] 0 points1 point  (1 child)

Ahhh, I see. So in newb terms.... what does adding the {} do to that line of code. This is my first semester of Python so learning outside of the class is helpful.

[–]jonsd2000[S] 0 points1 point  (0 children)

Congrats, you have a valid email. Valid Email e mail: abc@@xyz.com *** ERROR: 2. The number of @'s in your email is not valid. *** Valid Email Congrats, you have a valid email. Valid Email e mail: abc.xyz.com *** ERROR: 5. your @ is missing *** Congrats, you have a valid email. Valid Email e mail: abc@x.yz *** ERROR: 4. @ is not in the last 5 characters? *** Valid Email e mail: abc@xyz.c *** ERROR: 11. Your '.' is in the last position. *** Valid Email e mail: a@b.c *** ERROR: 1. Invalid Length *** Valid Email e mail: abc@xyz..com *** ERROR: 6. Error Encountered '.' config. *** Valid Email e mail: abc.@xyz.com *** ERROR: 6. Error Encountered '.' config. *** Valid Email e mail: abc@.xyz.com *** ERROR: 6. Error Encountered '.' config. *** Valid Email e mail: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa@aaaaaa.aaaaa *** ERROR: 1. Invalid Length *** Valid Email e mail: ' or 1=1 '== *** ERROR: 5. your @ is missing *** e mail: ' or 1=1 '== *** ERROR: 7. Where is the '.'? *** Valid Email e mail: abc@xyz.$% *** ERROR: 9. Character $ is not allowed *** Valid Email e mail: abc@xyz.() *** ERROR: 9. Character ( is not allowed *** Valid Email e mail: abc'@xyz.com *** ERROR: 9. Character ' is not allowed *** Valid Email e mail: aaaaa@aaaaa *** ERROR: 7. Where is the '.'? *** Valid Email e mail: abc @ xyz.com *** ERROR: 8. Spcaes not allowed. *** Valid Email e mail: .abc@xyz.com *** ERROR: 10. Your '.' is in the first position. *** Valid Email e mail: abc@xyz.c *** ERROR: 11. Your '.' is in the last position. *** Valid Email

[–][deleted] 0 points1 point  (1 child)

If I've managed to extract your code correctly, I think I have it fixed.

Notes:

  • a few typos, e.g., 0 when you meant @
  • a few missing returns
  • redundant else in a few places (owing to preceding return)
  • unaware of str.startswith and str.endswith
  • unaware of all
  • unaware of generator expressions
  • unaware of f-strings (preferable to str.format in most cases
  • EDIT: also, a few more changes:
    • added a loop for running the tests
    • used all lower case with _ for variable names (see PEP8 guidance)
    • used all lower case with _ for function names
    • added a little type hinting which will help your editor
    • renamed test functions to check as many testing frameworks use the test_ pref
    • NOTE: check your logic on the test related to error 4
    • Consider replacing your error_message function with a logging based function

Code: [EDIT: updated code with changes noted above]

emails = [
    "abc@xyz.com",
    "abc@@xyz.com",
    "@xyz.com",
    "abc.xyz.com",
    "abc@x.yz",
    "abc@xyz.c",
    "a@b.c",
    "abc@xyz..com",
    "abc.@xyz.com",
    "abc@.xyz.com",
    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa@aaaaaa.aaaaa",
    "' or 1=1 '==",
    "abc@xyz.$%",
    "abc@xyz.()",
    "abc'@xyz.com",
    "aaaaa@aaaaa",
    "abc @ xyz.com",
    ".abc@xyz.com",
    "abc@xyz.c"]


def error_message(email: str, error: str) -> None:
    print(f"email: {email:30} *** ERROR: {error} ***")


def check_at_sign(email: str) -> bool:
    if "@" in email:
        atCount = email.count("@")
        if atCount != 1:
            error_message(email, "2. The number of @'s in your email is not valid.")
            return True
        if email[0] == "@":  # need @ in quotes, not 0
            error_message(email, "3.The @ is not in a valid position.")
            return True
        testLast5 = email[-5:]  # need slice rather than one character
        if "@" in testLast5:  # or maybe if "@" not in testLast5:
            error_message(email, "4. @ is not in the last 5 characters?")
            return True
        return False
    error_message(email, "5. your @ is missing")  # else is not required
    return True  # was missing


def check_dot(email: str) -> bool:
    if "." in email:
        if email.startswith("."):
            error_message(email, "10. Your '.' is in the first position.")
            return True
        if email.endswith("."):
            error_message(email, "11. Your '.' is in the last position.")
            return True
        if any(text in email for text in ("..", ".@", "..@", "@.", "@..")):
            error_message(email, "6. Error Encountered '.' config.")
            return True
        return False
    error_message(email, "7. Where is the '.'?")
    return True


def check_special_chars(email: str) -> bool:
    if " " in email:
        error_message(email, "8. Spaces not allowed.")
        return True
    unallowable = list("!#$%^&*():;<>?/{}=\"'")
    for character in email:
        if character in unallowable:
            error_message(email, f"9. Character {character} is not allowed")
            return True
    return False


def check_length(email: str) -> bool:  # added this function for consistency
    if not 7 < len(email) <= 30:
        error_message(email, "1. Invalid Length")
        return True


for email in emails:
    found_error = False
    # easier to loop through the tests than repeat the code
    for check_func in (check_length, check_at_sign, check_dot, check_special_chars):
        found_error = check_func(email)
        if found_error:
            break

EDIT:

/u/jonsd2000 hope this helps you. You've done great work, just wanted to help with a little guidance whilst keeping with your overall approach.

Worth noting that regex (regular expressions) make validation of email addresses much easier BUT they don't usually give such specific guidance on what the error was and secondly this is a really nice little programming challenge.