我尝试了异常处理并且陷入了我的第一个程序,在这个程序中我的第一个继续工作,而第二个工作没有继续循环
print("hello to divide")
o = "y"
while o == "y":
try:
x = int(input("enter first no. = "))
y = int(input("enter second no. = "))
except:
print("please enter numeric value")
continue
try:
z = x/y
print(str(x) +"/"+str(y)+"="+str(z))
except:
print("please do not divide with 0(zero)")
continue
finally:
o = input("do you want to do it again (y/n)? = ")
第二个除了工作正常但在打印消息后它跳转到finally语句
请帮忙???
答案 0 :(得分:1)
来自docs:
finally子句总是在离开
try
之前执行 声明,是否发生了异常。当一个例外 发生在try
条款中,并且没有被处理过except
子句(或者它出现在except
或else
子句中, 在finally
子句执行后重新引发它。该finally
子句也在其他任何时候“在出路上”执行 try语句的子句通过break
,continue
或return
声明。一个更复杂的例子:
我很确定你只是想要:
print("hello to divide")
o = "y"
while o == "y":
try:
x = int(input("enter first no. = "))
y = int(input("enter second no. = "))
except:
print("please enter numeric value")
continue
try:
z = x/y
print(str(x) +"/"+str(y)+"="+str(z))
except:
print("please do not divide with 0(zero)")
continue
o = input("do you want to do it again (y/n)? = ")