如何处理嵌套函数和异常中的 None 值

How to handle None value in nested functions and exception

更新为

我的目标是

  1. 只有当 x 和 y 都不是 None
  2. 时才会调用 q = getQuotient() 行
  3. 获取 calc1() 停止 运行ning 并在获取 ZeroDivisionError 后返回到 运行 b.calc2()。
  4. to get b.calc2() 当 x 和 y 可以是 None 以及包括 0.[=33= 的数字时,无论 b.calc1() 是否陷入 ZeroDivisionException 中,都会执行行]

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

我的目标是

  1. 如果只有 x 和 y 不是 None
  2. ,则调用 q = getQuotient() 行
  3. to get b.calc2() 当 x 和 y 可以是 None 以及包括 0.[=33= 的数字时,无论 b.calc1() 是否陷入 ZeroDivisionException 中,都会执行行]

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.

# ...
  1. 我不明白 'not x',但你为什么不直接输入:

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

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

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.