0

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:

  1. Not having an error in the division, as the division 5/3 makes sense
  2. 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)
Mauro
  • 477
  • 1
  • 9
  • 22
  • 2
    After you redefine `y` you don't redo the division before `print(z)` – Barmar Feb 09 '21 at 18:13
  • Hint: Lines above execute before lines below. The attempt to assign `z` happens before the second loop, and fails. Generally speaking, avoid blanket `except` statements, as they will hide bugs and introduce new ones. – tripleee Feb 09 '21 at 18:14
  • Possible duplicate of https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response – tripleee Feb 09 '21 at 18:19

2 Answers2

3

IMO your code is a bit over-complicated. My understanding is that you actually want the program to end once the user provides any non-zero input. Thus, you only need a single while True. Also, I don't quite understand why you force the code to throw ZeroDivisionError before even the user provides any input. You can omit the part where you initialise y to 0 and let the user provide a command-line argument from the very beginning.


The following should do the trick:

x = 5

while True:
    try:
        y = int(input("y = __"))
        z = x / y
    except ZeroDivisionError:
        print("error in the division. Please, redefine y")
        continue
    else:
        print("All done")
        print(z)
        break
Giorgos Myrianthous
  • 36,235
  • 20
  • 134
  • 156
  • It is a simplifying solution, I am editing my answer though since I found the solution for my specific code. I have to declare y = 0 before initalizing the loop because it is a requirement for the assignment – Mauro Feb 09 '21 at 18:27
2

You're trying to print z before you repeat the outer loop, so you haven't performed the division with the corrected y. Take that out of the loop, it should be after the outer loop so that it waits for the division to succeed and for the user to enter a valid replacement.

And use break to break out of the main loop when the division is successful.

x = 5
y = 0

while True:
    try:
        z = x/y
        break
    except:
        while True:
            try:
                print("error in the division. Please, redefine y")
                y = int(input("y = __"))
            except:
                print("Redefine y properly__")
                continue

print("All done")
print(z)
break
Barmar
  • 741,623
  • 53
  • 500
  • 612