Python:为什么这个 Python 控制台应用程序会出现这个错误?

Python: why does this error occur in this Python console app?

我在 Python 中开发了一个控制台应用程序,我想检查有效的电子邮件提供商,例如。 gmail.com。问题是当我添加这一行时;

def print_valid(address):
    points = 0
    count = 0
    chars = [char for char in address]
    unallowed_symbols = ["#", "$", "%", "^", "&", "*", "/"]
    email_providers = ["@outlook", "@gmail", "@hotmail", "@yahoo"]

    # point one
    if "@" in address:
        points += 1
        
    # point two
    for letters in chars:
        if "@" in letters:
            count += 1

    if count < 1 or count > 1:
        points -= 1 
    else: 
        points += 1

    # point three
    for symbols in unallowed_symbols:
        if symbols in address:
            points -= 1
        else:
            break
    points += 1

    # point four
    for provider in email_providers:
        if provider in address:
            points += 1
        else: # this line
            points -= 1 # this line

    # check points
    if points == 4:
        return "Valid address"
    else: 
        return "Invalid address"

email = str(input("Enter email: "))
print(print_valid(email))

当我添加这两行并输入无效的电子邮件时,例如。 @fake.com 说无效,但对@gmail.com 它也输出无效。我不想让你更正我的代码。我只想知道为什么会出现这个错误。

谢谢

您将 point four 中的 points 减少了 3 次。

代码:

def print_valid(address):
    points = 0
    count = 0
    chars = [char for char in address]
    unallowed_symbols = ["#", "$", "%", "^", "&", "*", "/"]
    email_providers = ["@outlook", "@gmail", "@hotmail", "@yahoo"]

    # point one
    if "@" in address:
        points += 1
        
    # point two
    for letters in chars:
        if "@" in letters:
            count += 1

    if count < 1 or count > 1:
        points -= 1 
    else: 
        points += 1

    # point three
    for symbols in unallowed_symbols:
        if symbols in address:
            points -= 1
        else:
            break
    points += 1

    # point four
    for provider in email_providers:
        if provider in address:
            points += 1
            break
        # else: # this line
        #     points -= 1 # this line

    # check points
    if points == 4:
        return "Valid address"
    else: 
        return "Invalid address"

# email = str(input("Enter email: "))
email = "ex. @fake.com"
print(print_valid(email))
email = "@gmail.com"
print(print_valid(email))

结果:

Invalid address
Valid address