Oct-05-2019, 02:04 AM
Hi!
I'm trying (no pun intended!
) to understand the try/except blocks. I'm using this program, that returns "Error: Invalid argument.", when the division is tried to be done by zero:
Thanks in advance.
All the best,
I'm trying (no pun intended!

def division1(divideBy): try: return 72 / divideBy except ZeroDivisionError: print(f'72 divided by 0 produces an Error: Invalid argument.') print(f'72 divided by 2 is {division1(2)}.') print(f'72 divided by 12 is {division1(12)}.') print(f'72 divided by 0 is {division1(0)}.') print(f'72 divided by 1 is {division1(1)}.')that produces the following output:
Output:72 divided by 2 is 36.0.
72 divided by 12 is 6.0.
72 divided by 0 produces an Error: Invalid argument.
72 divided by 0 is None.
72 divided by 1 is 72.0.
but I would like the line returning the value None
not to appear in the output, like this:Output:72 divided by 2 is 36.0.
72 divided by 12 is 6.0.
72 divided by 0 produces an Error: Invalid argument.
72 divided by 1 is 72.0.
How could I do it? I mean, of course, not dividing by zero, but I want just the program to say that that would produce an error due to an invalid argument.Thanks in advance.
All the best,