代码之家  ›  专栏  ›  技术社区  ›  Vanjith

方法返回None,即使在python 3中value不是None[duplicate]

  •  0
  • Vanjith  · 技术社区  · 7 年前

    这个问题已经有了答案:

    我在运行时得到输入。如果第一次尝试自己纠正给定的输入意味着我没有得到错误。否则它会显示 无法打开不可读取的 错误。

    我能看到它在发送 None 如果递归方法调用一次。即使在返回之前它也会打印值,但在接收之后它不会显示任何值。

    class Game:
        def set_val(self):
            try:
                p1=input("Enter player 1 name:")
                p2=input("Enter player 2 name:")
                goal=int(input("Enter a number to set goal:"))
                if p1 is None or p2 is None or goal is None:
                    print("Please give proper input")
                    self.set_val()
                else:
                    print(p1,p2,goal)
                    return p1,p2,goal
            except:
                print("Please give proper input")
                self.set_val()
    G=Game()
    
    p1,p2,goal=G.set_val()
    print(p1,p2,goal)
    

    输出:

    Enter player 1 name:s
    Enter player 2 name:c
    Enter a number to set goal:s
    Please give proper input
    Enter player 1 name:s
    Enter player 2 name:v
    Enter a number to set goal:2
    s v 2
    Traceback (most recent call last):
      File "D:\test.py", line 18, in <module>
        p1,p2,goal=G.set_val()
    TypeError: cannot unpack non-iterable NoneType object
    

    我可以假设它是因为递归调用,但无法找出原因。请解释给出一个解决方案,以获得所有输入值没有错误。

    2 回复  |  直到 7 年前
        1
  •  2
  •   Mark Tolonen    7 年前

    递归有它的用途,但使用它重新开始不是其中之一。光着身子也不好。

    如果需要循环,请使用循环。

    如果需要捕获异常,请捕获 预期 例外。这样,如果发生意外异常,您就可以看到并处理该bug。

    例子:

    class Game:
        def set_val(self):
            while True:
                p1=input("Enter player 1 name:")
                if p1: break
            while True:
                p2=input("Enter player 2 name:")
                if p2: break
    
            # Loop until good input
            # int() will throw ValueError if input is not convertible to int.
            while True:
                try:
                    goal=int(input("Enter a number to set goal:"))
                    break # break from the loop if no exception
                except ValueError:
                    print("Please give proper input")
    
            return p1,p2,goal
    
    G=Game()
    
    p1,p2,goal=G.set_val()
    print(p1,p2,goal)
    
        2
  •  2
  •   Error - Syntactical Remorse user8194964    7 年前

    1)永远不要做一般的例外来捕捉所有的东西。

    2)用户输入不能使用递归。它无缘无故地消耗堆栈。就用一个 while 循环基本上递归并不是为了在失败时保持重复。它被设计成潜入某物并最终返回。

    3)确保代码空间正确。这并没有引起问题,但确实使代码难以阅读。你应该有一个前后运算符的空格(即。 = )逗号后的空格,包含多个元素(即。 (1, 2, 3) )

    也就是说,当进行递归时,需要返回递归调用:

    class Game:
        def set_val(self):
            try:
                p1 = input("Enter player 1 name:")
                p2 = input("Enter player 2 name:")
                goal = int(input("Enter a number to set goal:"))
                if p1 is None or p2 is None or goal is None:
                    print("Please give proper input")
                    return self.set_val()
                else:
                    print(p1, p2, goal)
                    return p1, p2, goal
            except:
                print("Please give proper input")
                return self.set_val()
    G = Game()
    
    p1, p2, goal = G.set_val()
    print(p1, p2, goal)
    

    还有你的 if 可以缩短为:

    if p1 and p2 and goal:
    
    推荐文章