【问题标题】:How to handle None value in nested functions and exception如何处理嵌套函数和异常中的 None 值
【发布时间】:2020-07-31 19:03:38
【问题描述】:

更新到

我的目标是

  1. 只有当 x 和 y 都不是 None 时才调用获取 q = getQuotient() 行
  2. 在得到 ZeroDivisionError 后让 calc1() 停止运行并返回 main 运行 b.calc2()。
  3. 当 x 和 y 可以为 None 以及包括 0 的数字时,无论 b.calc1() 是否被捕获到 ZeroDivisionException 中,都会执行 b.calc2() 行。

我在下面的 cmets 中添加了我的问题。

我的目标是

  1. 如果只有 x 和 y 不是 None,则调用 q = getQuotient() 行
  2. 当 x 和 y 可以为 None 以及包括 0 的数字时,无论 b.calc1() 是否被捕获到 ZeroDivisionException 中,都会执行 b.calc2() 行。

a.py

import b
if __name__ == "__main__":
    b.calc1()
    print("Done with 1")
    b.calc2()
    print("Done with 2")

b.py

def calc1():
    x = getX()
    y = getY()
    if not x or not y:
        return

    q = getQuotient(x, y)
    if q:
        print(q)
    # does more stuff here
    # and I would like this calc1() to stop here, stop running if getQuotient() gets the ZeroDivisionError.

def getQuotient(x, y):
    try:
        return x/y
    except ZeroDivisionError:
        print("zero division error happened.")
    # what can, should I do here? 
    # I would like calc1() to stop running and go back to main if it gets ZeroDivisionError.
    # How's "return None" since "if quotient" line will stop processing any further if y is zero?
    # if I raise, it terminates the program, which means b.calc2() won't run. I need it to run, though.

# ...

【问题讨论】:

    标签: python exception nonetype divide-by-zero nested-function


    【解决方案1】:

    a.py

    import b
    if __name__ == "__main__":
        b.calc1()
        print("Done with 1")
        b.calc2()
        print("Done with 2")
    

    b.py

        def calc1():
        x = getX()
        y = getY()
        if not x or not y:
            return
    
        q = getQuotient(x, y)
        if not q:
            return
        else:
            # The other lines 
            # of code to run if it
            # is not 'None'
       
    def getQuotient(x, y):
        try:
            return x/y
        except ZeroDivisionError:
            print("zero division error happened.")
    
    P.S using 'return None' will also do in your version of b.py but it will return None automatically in case of exception.
        
    

    【讨论】:

    • 嗨!谢谢你的建议。如果可能,我想将 ZeroDivisionError 异常保留在 getQuotient 函数中。我也更新了一些目标/代码,你能再看看吗?
    • 这样就可以了。你不需要在这里引发任何异常或返回None,如果发生异常,它将默认返回None
    • 如果在“if not x and/or not y”之后还有更多行,它会在陷入异常后继续运行,不是吗?进入异常后如何摆脱 calc1() ?
    • 我已经对我的答案进行了更改。如果出现异常,它将只返回到 main 否则它将运行以下代码行
    【解决方案2】:
    1. 我不明白'not x',但你为什么不直接输入:

      如果 x 和 y: q = getQuotient(x, y)

    2. ZeroDivisionError 被捕获,因此代码将打印它发生并继续前进。

    【讨论】:

    • 1.抱歉有一个错字,我的意思是*如果不是 x 而不是 y。这是为了目标#1。 2. 我需要它在 ZeroDivisionError 之后停止处理 calc1(),然后返回 main,打印“Done with 1”并继续使用 b.calc2()。我怎样才能做到这一点?
    • 已调整。如果我错过了什么,请告诉我
    • 如果 x 和 y 都为 None,getQuotient 不会运行 1,不是吗?对于 2,目前,在 "q = getQuotient(x, y)" 行之后,q 为 None 并且 calc1() 继续运行并执行 #... 行。我想找到一种方法让 calc1() 停止运行并返回到 main :'(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-01
    • 1970-01-01
    • 2023-02-10
    • 1970-01-01
    • 2021-04-25
    相关资源
    最近更新 更多