【发布时间】:2020-07-31 19:03:38
【问题描述】:
更新到
我的目标是
- 只有当 x 和 y 都不是 None 时才调用获取 q = getQuotient() 行
- 在得到 ZeroDivisionError 后让 calc1() 停止运行并返回 main 运行 b.calc2()。
- 当 x 和 y 可以为 None 以及包括 0 的数字时,无论 b.calc1() 是否被捕获到 ZeroDivisionException 中,都会执行 b.calc2() 行。
我在下面的 cmets 中添加了我的问题。
我的目标是
- 如果只有 x 和 y 不是 None,则调用 q = getQuotient() 行
- 当 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