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

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

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地址
在一个皱眉变成笑脸之前无法检查

这是代码:

import re

def main():

    print(validate(input("IPv4 Address: ")))

def validate(ip):
    address = "(0|[1-9]|[1-9][0-9]|[1-2][0-5][0-5]|1[0-9][0-9]|2[0-4][0-9])"
    if re.search(fr'^{address}\.{address}\.{address}\.{address}$', ip):
        return "True"
    else:
        return "False"

if __name__ == "__main__":
    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:

:( correct numb3rs.py passes all test_numb3rs.py checks
    expected exit code 0, not 1
:| test_numb3rs.py catches numb3rs.py only checking if first byte of IPv4 address is in range
    can't check until a frown turns upside down
:| test_numb3rs.py catches numb3rs.py accepting expecting five-byte IPv4 address
    can't check until a frown turns upside down
import re

def main():

    print(validate(input("IPv4 Address: ")))

def validate(ip):
    address = "(0|[1-9]|[1-9][0-9]|[1-2][0-5][0-5]|1[0-9][0-9]|2[0-4][0-9])"
    if re.search(fr'^{address}\.{address}\.{address}\.{address}$', ip):
        return "True"
    else:
        return "False"

if __name__ == "__main__":
    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中,您可以使用以下命令退出程序并指定退出代码:

import sys
sys.exit(100)

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

import re, sys

def validate(ip):
    address = "(0|[1-9]|[1-9][0-9]|[1-2][0-5][0-5]|1[0-9][0-9]|2[0-4][0-9])"
    if re.search(fr'^{address}\.{address}\.{address}\.{address}$', ip):
        return True
    else:
        return False

if __name__ == "__main__":
    v =  validate(input("IPv4 Address: "))
    print(v)
    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:

import sys
sys.exit(100)

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

import re, sys

def validate(ip):
    address = "(0|[1-9]|[1-9][0-9]|[1-2][0-5][0-5]|1[0-9][0-9]|2[0-4][0-9])"
    if re.search(fr'^{address}\.{address}\.{address}\.{address}$', ip):
        return True
    else:
        return False

if __name__ == "__main__":
    v =  validate(input("IPv4 Address: "))
    print(v)
    sys.exit(int(v))

答案2

得分: -2

以下是代码的翻译结果:

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

import re

def main():
    print(validate(input("IPv4地址:")))

def validate(ip):
    if re.search(r"^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$", ip):
        list_of_numbers = ip.split(".")
        for number in list_of_numbers:
            if int(number) < 0 or int(number) > 255:
                return False
        return True
    else:
        return False

if __name__ == "__main__":
    main()

以及test_numb3rs.py的内容:

from numb3rs import validate

def main():
    test_format()
    test_range()

def test_format():
    assert validate(r"1.2.3.4")==True
    assert validate(r"1.2.3")==False
    assert validate(r"1.2")==False
    assert validate(r"1")==False

def test_range():
    assert validate(r"255.255.255.255")==True
    assert validate(r"512.1.1.1")==False
    assert validate(r"1.512.1.1")==False
    assert validate(r"1.1.512.1")==False
    assert validate(r"1.1.1.512")==False

if __name__ == "__main__":
    main()
英文:

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

 import re


 def main():
   print(validate(input(&quot;IPv4 Address: &quot;)))


 def validate(ip):

if re.search(r&quot;^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$&quot;, ip):
    list_of_numbers = ip.split(&quot;.&quot;)
    for number in list_of_numbers:
        if int(number) &lt; 0 or int(number) &gt; 255:
            return False
    return True
else:
    return False



  if __name__ == &quot;__main__&quot;:
    main()

and the test_numb3rs.py:

  from numb3rs import validate

    def main():
     test_format()
     test_range()


  def test_format():
      assert validate(r&quot;1.2.3.4&quot;)==True
      assert validate(r&quot;1.2.3&quot;)==False
      assert validate(r&quot;1.2&quot;)==False
      assert validate(r&quot;1&quot;)==False

  def test_range():
      assert validate(r&quot;255.255.255.255&quot;)==True
      assert validate(r&quot;512.1.1.1&quot;)==False
      assert validate(r&quot;1.512.1.1&quot;)==False
      assert validate(r&quot;1.1.512.1&quot;)==False
      assert validate(r&quot;1.1.1.512&quot;)==False



  if __name__ == &quot;__main__&quot;:
      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:

确定