【发布时间】:2018-11-04 20:07:18
【问题描述】:
global r
global t
def divide(a,b):
try:
c=a/b
print(c)
return c
except:
b+=1000
r=b
raise Exception
def main():
try:
r=0
t=1000
divide(t,r)
except:
print('except block')
divide(t,r)
main()
如何更改变量 r 的值,然后在将其捕获为异常后再次从 except 块中调用该函数?
【问题讨论】:
-
从它自己的
except块调用函数?使用except ZeroDivisionError:顺便说一句。 -
对于@Jean-François Fabre 的评论:
globals通常应避免使用。另外,为什么不把所有这些都放在一个类中呢? -
@AdamMitchell 上课?用于划分 2 个数字?
-
@DeepSpace stackoverflow.com/questions/19158339/…
-
@AdamMitchell OP 不应该使用全局变量这一事实并不能证明在这里使用类是合理的。请参阅 John 对此问题的回答。
标签: python python-3.x exception try-catch try-except