Aekanshu
Aekanshu

Reputation: 91

Python with 2 continue in a while

I tried exception handling and got stuck in my first program, in this program my first continue in while is working but 2nd one is not continuing the loop

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)? = ")

The second except is working fine but after printing message it jumps to finally statement

please help ???

Upvotes: 0

Views: 49

Answers (1)

juanpa.arrivillaga
juanpa.arrivillaga

Reputation: 96277

From the 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:

I'm pretty sure you just want:

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)? = ")

Upvotes: 1

Related Questions