用Python 2继续在一段时间
问题描述:
我试过异常处理和陷在了我的第一个程序,在这个程序我第一次继续在同时工作,但第二个一个不连续的环用Python 2继续在一段时间
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)? = ")
第二个不同之处工作正常,但打印后的消息,它跳转到最后声明
请帮忙?
答
从docs:
一个最后条款离开
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)? = ")
我试图格式化你的代码,但你应该确保它准确地反映了你确实有。 –
你为什么继续使用? – scharette
是的,因为'finally'总是被执行。这就是整个问题。 –