Python with 2 稍后继续
Python with 2 continue in a while
我尝试了异常处理并卡在了我的第一个程序中,在这个程序中我的第一个 continue in while 正在工作但第二个没有继续循环
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)? = ")
第二个 except 工作正常,但在打印消息后跳转到 finally 语句
请帮忙???
来自docs:
A finally clause is always executed before leaving the try
statement, whether an exception has occurred or not. When an exception
has occurred in the try
clause and has not been handled by an
except
clause (or it has occurred in an except
or else
clause),
it is re-raised after the finally
clause has been executed. The
finally
clause is also executed “on the way out” when any other
clause of the try statement is left via a break
, continue
or
return
statement. A more complicated example:
我很确定你只是想要:
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)? = ")
我尝试了异常处理并卡在了我的第一个程序中,在这个程序中我的第一个 continue in while 正在工作但第二个没有继续循环
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)? = ")
第二个 except 工作正常,但在打印消息后跳转到 finally 语句
请帮忙???
来自docs:
A finally clause is always executed before leaving the
try
statement, whether an exception has occurred or not. When an exception has occurred in thetry
clause and has not been handled by anexcept
clause (or it has occurred in anexcept
orelse
clause), it is re-raised after thefinally
clause has been executed. Thefinally
clause is also executed “on the way out” when any other clause of the try statement is left via abreak
,continue
orreturn
statement. A more complicated example:
我很确定你只是想要:
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)? = ")