check50期望在numb3rs.py中的退出代码为0而不是1。

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

check50 expects exit code 0 not 1 in numb3rs.py

问题

我会为你翻译以下内容:

我正在尝试解决cs50的Python编程入门问题集7中的numb3rs问题,其中我需要检查输入的IP地址是否有效。问题是check50给了我三个我不理解的错误消息。这些是三个错误:

check50期望在numb3rs.py中的退出代码为0而不是1。 正确的numb3rs.py通过了所有的test_numb3rs.py检查
预期的退出代码是0,而不是1
check50期望在numb3rs.py中的退出代码为0而不是1。 test_numb3rs.py只检查numb3rs.py的IPv4地址的第一个字节是否在范围内
在一个皱眉变成笑脸之前无法检查
check50期望在numb3rs.py中的退出代码为0而不是1。 test_numb3rs.py捕捉到numb3rs.py接受期望的五字节IPv4地址
在一个皱眉变成笑脸之前无法检查

这是代码:

  1. import re
  2. def main():
  3. print(validate(input("IPv4 Address: ")))
  4. def validate(ip):
  5. address = "(0|[1-9]|[1-9][0-9]|[1-2][0-5][0-5]|1[0-9][0-9]|2[0-4][0-9])"
  6. if re.search(fr'^{address}\.{address}\.{address}\.{address}$', ip):
  7. return "True"
  8. else:
  9. return "False"
  10. if __name__ == "__main__":
  11. main()

这是代码。我尝试使用sys在打印后退出程序,但不起作用。我还尝试将True和False作为布尔值返回,但也不起作用。我还按照问题的要求编写了test_numb3rs.py,并检查了所有内容(所有字节,用户输入是否为IP地址等)。

英文:

The problem I'm trying is cs50's introduction to programming with python problem set 7 numb3rs problem, wherein I need to check if the Ip address entered is a valid Ip address or not. The thing is check50 gives me three error messages that I do not understand. These are the three errors:

  1. :( correct numb3rs.py passes all test_numb3rs.py checks
  2. expected exit code 0, not 1
  3. :| test_numb3rs.py catches numb3rs.py only checking if first byte of IPv4 address is in range
  4. can't check until a frown turns upside down
  5. :| test_numb3rs.py catches numb3rs.py accepting expecting five-byte IPv4 address
  6. can't check until a frown turns upside down
  1. import re
  2. def main():
  3. print(validate(input("IPv4 Address: ")))
  4. def validate(ip):
  5. address = "(0|[1-9]|[1-9][0-9]|[1-2][0-5][0-5]|1[0-9][0-9]|2[0-4][0-9])"
  6. if re.search(fr'^{address}\.{address}\.{address}\.{address}$', ip):
  7. return "True"
  8. else:
  9. return "False"
  10. if __name__ == "__main__":
  11. main()

This is the code. I tried using sys to exit the program after print but that does not work. I also tried returning True and False as bools but that also didn't work. I also did a test_numb3rs.py as the problem asked for it and checked everything(All bytes, if user input was not an Ip address, and so on).

答案1

得分: -2

程序退出代码在正常情况下应为1,不正常情况下应为0。

在Python中,您可以使用以下命令退出程序并指定退出代码:

  1. import sys
  2. sys.exit(100)

因此,您可以将此代码添加到您的程序中,或者按照以下方式更新您的代码:

  1. import re, sys
  2. def validate(ip):
  3. address = "(0|[1-9]|[1-9][0-9]|[1-2][0-5][0-5]|1[0-9][0-9]|2[0-4][0-9])"
  4. if re.search(fr'^{address}\.{address}\.{address}\.{address}$', ip):
  5. return True
  6. else:
  7. return False
  8. if __name__ == "__main__":
  9. v = validate(input("IPv4 Address: "))
  10. print(v)
  11. sys.exit(int(v))
英文:

A program exit code should be 1 when OK and 0 when not ok.

In Python you can exit your program with an exit code using the command:

  1. import sys
  2. sys.exit(100)

So you can just add that in your code,
or update your code this way:

  1. import re, sys
  2. def validate(ip):
  3. address = "(0|[1-9]|[1-9][0-9]|[1-2][0-5][0-5]|1[0-9][0-9]|2[0-4][0-9])"
  4. if re.search(fr'^{address}\.{address}\.{address}\.{address}$', ip):
  5. return True
  6. else:
  7. return False
  8. if __name__ == "__main__":
  9. v = validate(input("IPv4 Address: "))
  10. print(v)
  11. sys.exit(int(v))

答案2

得分: -2

以下是代码的翻译结果:

这是我的代码,并且我通过了所有的测试:

  1. import re
  2. def main():
  3. print(validate(input("IPv4地址:")))
  4. def validate(ip):
  5. if re.search(r"^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$", ip):
  6. list_of_numbers = ip.split(".")
  7. for number in list_of_numbers:
  8. if int(number) < 0 or int(number) > 255:
  9. return False
  10. return True
  11. else:
  12. return False
  13. if __name__ == "__main__":
  14. main()

以及test_numb3rs.py的内容:

  1. from numb3rs import validate
  2. def main():
  3. test_format()
  4. test_range()
  5. def test_format():
  6. assert validate(r"1.2.3.4")==True
  7. assert validate(r"1.2.3")==False
  8. assert validate(r"1.2")==False
  9. assert validate(r"1")==False
  10. def test_range():
  11. assert validate(r"255.255.255.255")==True
  12. assert validate(r"512.1.1.1")==False
  13. assert validate(r"1.512.1.1")==False
  14. assert validate(r"1.1.512.1")==False
  15. assert validate(r"1.1.1.512")==False
  16. if __name__ == "__main__":
  17. main()
英文:

here are my codes, and i pass all the tests:

  1. import re
  2. def main():
  3. print(validate(input(&quot;IPv4 Address: &quot;)))
  4. def validate(ip):
  5. if re.search(r&quot;^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$&quot;, ip):
  6. list_of_numbers = ip.split(&quot;.&quot;)
  7. for number in list_of_numbers:
  8. if int(number) &lt; 0 or int(number) &gt; 255:
  9. return False
  10. return True
  11. else:
  12. return False
  13. if __name__ == &quot;__main__&quot;:
  14. main()

and the test_numb3rs.py:

  1. from numb3rs import validate
  2. def main():
  3. test_format()
  4. test_range()
  5. def test_format():
  6. assert validate(r&quot;1.2.3.4&quot;)==True
  7. assert validate(r&quot;1.2.3&quot;)==False
  8. assert validate(r&quot;1.2&quot;)==False
  9. assert validate(r&quot;1&quot;)==False
  10. def test_range():
  11. assert validate(r&quot;255.255.255.255&quot;)==True
  12. assert validate(r&quot;512.1.1.1&quot;)==False
  13. assert validate(r&quot;1.512.1.1&quot;)==False
  14. assert validate(r&quot;1.1.512.1&quot;)==False
  15. assert validate(r&quot;1.1.1.512&quot;)==False
  16. if __name__ == &quot;__main__&quot;:
  17. main()

huangapple
  • 本文由 发表于 2023年8月9日 05:06:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76863194.html
匿名

发表评论

匿名网友

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

确定