Reputation: 91
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
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 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:
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