Python与2继续一段时间

时间:2017-11-03 21:51:59

标签: python exception while-loop exception-handling continue

我尝试了异常处理并且陷入了我的第一个程序,在这个程序中我的第一个继续工作,而第二个工作没有继续循环

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语句

请帮忙???

1 个答案:

答案 0 :(得分:1)

来自docs

  

finally子句总是在离开try之前执行   声明,是否发生了异常。当一个例外   发生在try条款中,并且没有被处理过   except子句(或者它出现在exceptelse子句中,   在finally子句执行后重新引发它。该   finally子句也在其他任何时候“在出路上”执行   try语句的子句通过 breakcontinue或   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)? = ")