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

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

Seeking assistacne with an on online Python course exercise please

问题

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

代码如下:

  1. def has_invalid_characters(string):
  2. valid = "abcdefghijklmnopqrstuvwxyz0123456789."
  3. # your code here
  4. for i in string:
  5. if i not in valid:
  6. return True
  7. else:
  8. return False
  9. def is_valid(email):
  10. if email.count("@") != 1:
  11. return False
  12. prefix, domain = email.split("@")
  13. if len(prefix) == 0:
  14. return False
  15. if domain.count(".") != 1:
  16. return False
  17. domain_name, extension = domain.split(".")
  18. if len(domain_name) == 0 or len(extension) == 0:
  19. return False
  20. if has_invalid_characters(prefix) == True:
  21. return False
  22. if has_invalid_characters(domain) == True:
  23. return False
  24. else:
  25. return True
  26. emails = [
  27. "test@example.com",
  28. "valid@gmail.com",
  29. "invalid@gmail",
  30. "invalid",
  31. "not an email",
  32. "invalid@email",
  33. "!@/",
  34. "test@@example.com",
  35. "test@.com",
  36. "test@site.",
  37. "@example.com",
  38. "an.example@test",
  39. "te#st@example.com",
  40. "test@exam!ple.com"
  41. ]
  42. for i in emails:
  43. is_valid(i)
  44. if i == True:
  45. print(i + " is valid")
  46. else:
  47. 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

  1. def has_invalid_characters(string):
  2. valid = "abcdefghijklmnopqrstuvwxyz0123456789."
  3. # your code here
  4. for i in string:
  5. if i not in valid:
  6. return True
  7. else:
  8. return False
  9. def is_valid(email):
  10. if email.count("@") != 1:
  11. return False
  12. prefix, domain = email.split("@")
  13. if len(prefix) == 0:
  14. return False
  15. if domain.count(".") != 1:
  16. return False
  17. domain_name, extension = domain.split(".")
  18. if len(domain_name) == 0 or len(extension) == 0:
  19. return False
  20. if has_invalid_characters(prefix) == True:
  21. return False
  22. if has_invalid_characters(domain) == True:
  23. return False
  24. else:
  25. return True
  26. emails = [
  27. "test@example.com",
  28. "valid@gmail.com",
  29. "invalid@gmail",
  30. "invalid",
  31. "not an email",
  32. "invalid@email",
  33. "!@/",
  34. "test@@example.com",
  35. "test@.com",
  36. "test@site.",
  37. "@example.com",
  38. "an.example@test",
  39. "te#st@example.com",
  40. "test@exam!ple.com"
  41. ]
  42. for i in emails:
  43. is_valid(i)
  44. if i == True:
  45. print(i + " is valid")
  46. else:
  47. 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()函数的调用不正确。

应该是

  1. for i in emails:
  2. result = is_valid(i)
  3. if result == True:
  4. .....

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

英文:

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

it should be

  1. for i in emails:
  2. result = is_valid(i)
  3. if result == True:
  4. .....

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:

确定