寻求在线Python课程练习的帮助,请。

huangapple go评论58阅读模式
英文:

Seeking assistacne with an on online Python course exercise please

问题

我一直在进行在线的Python课程,最后的练习是检查一组电子邮件地址中是否有无效的地址。

代码如下:

def has_invalid_characters(string):
    valid = "abcdefghijklmnopqrstuvwxyz0123456789."
    
    # your code here
    for i in string:
        if i not in valid:
            return True
        else:
            return False

def is_valid(email):
    
    if email.count("@") != 1:
        return False
    prefix, domain = email.split("@")
    if len(prefix) == 0:
        return False
    if domain.count(".") != 1:
        return False
    domain_name, extension = domain.split(".")
    if len(domain_name) == 0 or len(extension) == 0:
        return False
    if has_invalid_characters(prefix) == True:
        return False
    if  has_invalid_characters(domain) == True:
        return False
    else:
        return True

emails = [
    "test@example.com",
    "valid@gmail.com",
    "invalid@gmail",
    "invalid",
    "not an email",
    "invalid@email",
    "!@/",
    "test@@example.com",
    "test@.com",
    "test@site.",
    "@example.com",
    "an.example@test",
    "te#st@example.com",
    "test@exam!ple.com"
]
for i in emails:
    is_valid(i)
    if i == True:
        print(i + " is valid")
    else:
        print(i + " is invalid")

希望这可以帮助你找到问题。

英文:

I have been doing an online Python course and the final exercise was to check a list of email addresses for invalid addresses.

The code is

def has_invalid_characters(string):
    valid = "abcdefghijklmnopqrstuvwxyz0123456789."
    
    # your code here
    for i in string:
        if i not in valid:
            return True
        else:
            return False

def is_valid(email):
    
    if email.count("@") != 1:
        return False
    prefix, domain = email.split("@")
    if len(prefix) == 0:
        return False
    if domain.count(".") != 1:
        return False
    domain_name, extension = domain.split(".")
    if len(domain_name) == 0 or len(extension) == 0:
        return False
    if has_invalid_characters(prefix) == True:
        return False
    if  has_invalid_characters(domain) == True:
        return False
    else:
        return True

emails = [
    "test@example.com",
    "valid@gmail.com",
    "invalid@gmail",
    "invalid",
    "not an email",
    "invalid@email",
    "!@/",
    "test@@example.com",
    "test@.com",
    "test@site.",
    "@example.com",
    "an.example@test",
    "te#st@example.com",
    "test@exam!ple.com"
]
for i in emails:
    is_valid(i)
    if i == True:
        print(i + " is valid")
    else:
        print(i + " is invalid")

When I run this I am told that the first two email addresses, which should be reported as valid, are invalid, but I cannot figure out why. I have gone over it a few times and cannot see an error in the logic. I have also run it on my laptop and I get the same result.

In the course this code was written in steps and with the last step to change the for loop from simply printing the email addresses to validating them and everything until I modified the for was marked as correct.

I would be grateful if someone could point out the issue with this code to me.

答案1

得分: 0

我找到了问题。我的对is_valid()函数的调用不正确。

应该是

for i in emails:
    result = is_valid(i)
    if result == True:
    .....

对此问题给您带来麻烦,我很抱歉。

英文:

I found the issue. My call to the is_valid() function was not correct.

it should be

for i in emails:
    result = is_valid(i)
    if result == True:
    .....

Sorry for troubling you with this.

huangapple
  • 本文由 发表于 2023年2月7日 02:10:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365074.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定