I have the following simple code that aims to redefine the variable y
when it is mathematically impossible for it to be the divisor of x
:
x = 5
y = 0
while True:
try:
z = x/y
except:
while True:
try:
print("error in the division. Please, redefine y")
y = int(input("y = __"))
except:
print("Redefine y properly__")
continue
else:
print("All done")
print(z)
break
When I am asked to introduce the input and I introduce an integer like 3
I get the following ouput and the error:
error in the division. Please, redefine y y = __3 All done --------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) in 5 try: ----> 6 z = x/y 7 except:
ZeroDivisionError: division by zero
During handling of the above exception, another exception occurred:
NameError Traceback (most recent call last) in 15 else: 16 print("All done") ---> 17 print(z) 18 break 19
NameError: name 'z' is not defined
I expected:
- Not having an error in the division, as the division
5/3
makes sense - Not getting the error which says that name
z
is not defined, as it is defined in the 3rd line of the code:z = x/y
Could anyone help me? This is for an assignment of a Python course, so I am a beginner on it.
Edit: Beyond the solution provided below, I found the one to my specific code:
x = 5
y = 0
while True:
try:
z = x/y
break
except:
while True:
print("error in the division. Please, redefine y")
try:
y = int(input("y = __"))
break
except:
print("Redefine y properly__")
continue
print("All done")
print(z)