编写电子邮件验证器但遇到多个问题

问题描述 投票:0回答:1

email = [str(input("请输入电子邮件地址:"))]

def email_validator():

spaces = email.count(" ")
full_stop = email.count(".")
at = email.count("@")

incorrect_symbols = ('`!"£$%^&*()_-+=[]:;#~\|,<>/?')
for character in range(len(email)):
    if email(character) in incorrect_symbols:
        print("Your email has one or more incorrect symbols.")
        return False
    else:
        print("Your email doesn't have any incorrect symbols")
        return True

if full_stop != 0:
    print("Full stop detected! It saying: email has '.' - all right!")
    return True
elif full_stop == 0:
    print("Missed a full stop - wrong!")
    return False
else:
    print("Unsure!")
    return False

if at != 0:
    print("Your email has an '@'! - all right!")
    return True
elif at == 0:
    print("No @ detected - wrong!")
    return False
else:
    print("Unsure!")
    return False

if spaces == 0:
    print("Good job! No spaces detected! - all right!")
    return True
elif spaces != 0:
    print("No SPACES HERE - wrong")
    return False
else:
    print("Unsure!")
    return False

if full_stop > at:
    check = str(input("Was your email meant to have a full stop before the @?\nEnter Y or N: "))
    if check == Y:
        print("Your email has been verified - all right!")
    elif check == N:
        print("Oops. You've made a typo! - wrong!")
    else:
        print("I'm not quite sure what went wrong here - unsure!")
else:
    pass

if (full_stop_correct, at_correct, spaces_correct) == True:
    return True
else:
    return False
    

email_validator()

大家好

以上是我的代码。 该计划的要求是:

  • 确保输入字段中没有空格。
  • 确保其后有一个 (@) 符号和一个点 (.)。
  • 检查地址开头、中间和结尾部分的长度是否为空。
  • 当发现电子邮件地址无效时,请准确告诉用户他们的电子邮件地址出了什么问题,而不是仅仅说它无效。
  • 允许用户选择提供包含电子邮件地址列表的文本文件,并让它自动处理它们。

运行代码时出现多个错误。 其中包括:

Email Validator.py", line 11
    incorrect_symbols = ('`!"£$%^&*()_-+=[]:;#~\|,<>/?')
SyntaxWarning: invalid escape sequence '\|'
>>> 

Traceback (most recent call last):
  File "Email Validator.py", line 71, in <module>
    email_validator()
  File "Email Validator.py", line 13, in email_validator
    if email(character) in incorrect_symbols:
TypeError: 'list' object is not callable

任何帮助将不胜感激。

python email-validation
1个回答
0
投票
def email_validator(email):

    #Use double quotes or single quotes and declare the incorrect_symbols as string & use inside the function
    incorrect_symbols = "('`!\"£$%^&*()_-+=[]:;#~\|,<>/?"
    
    spaces = email.count(" ")
    full_stop = email.count(".")
    at = email.count("@")

    #In the following line, you used character as index. So better term is idx
    for idx in range(len(email)):

        #To denote the character , you should use square bracket
        if email[idx] in incorrect_symbols:

            return "Your email has one or more incorrect symbols."

        #function cant have multiple return statement parallelly. so use if, elif
        elif full_stop != 1:
            return 'Your email has either no full_stop or more than one full stop'
        
        elif at != 1:
            return "Your email has either no '@' symbol or more than one '@' symbol"
        
        elif spaces !=0:
            return 'The email id should not contain spaces'

        else:
            return '"Your email has been verified - all right!"' 


r = email_validator('[email protected]')
print(r)

#output : "Your email has been verified - all right!"
© www.soinside.com 2019 - 2024. All rights reserved.