Oct-05-2019, 04:59 AM
(This post was last modified: Oct-05-2019, 01:53 PM by newbieAuggie2019.)
(Oct-05-2019, 02:36 AM)wavic Wrote:def division1(divideBy): try: return 72 / divideBy except ZeroDivisionError: return
Thank you!
Your answer is much closer and nicer to what I want. The output is:
Output:72 divided by 2 is 36.0.
72 divided by 12 is 6.0.
72 divided by 0 is None.
72 divided by 1 is 72.0.
while I would prefer for the 3rd division to appear as '72 divided by 0 produces an Error: Invalid argument.', because in layman's terms, it explains what produces and why. '72 divided by 0 is None.' in layman's terms, it means '72 divided by 0 is 0.', which is misguiding:
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.
Thanks again!All the best,
(Oct-05-2019, 02:57 AM)Larz60+ Wrote: you should add the raise statement after handling the ZeroDivisionError, otherwise you will never see additional exceptions
def division1(divideBy): try: return 72 / divideBy except ZeroDivisionError: print(f'72 divided by 0 produces an Error: Invalid argument.') raise 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)}.')
Thank you!
Probably, I'm not very good at explaining. My apologies! The program I had, already warned with the message '72 divided by 0 produces an Error: Invalid argument.', but also included an extra line saying '72 divided by 0 is None.' that I don't like it, and I wish I could find a way to avoid it.
Your suggestions produce 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.
Error:Traceback (most recent call last):
File "C:/Users/User1/AppData/Local/Programs/Python/Python37/atbs_03_zeroDivide_02_BIS_03.py", line 10, in <module>
print(f'72 divided by 0 is {division1(0)}.')
File "C:/Users/User1/AppData/Local/Programs/Python/Python37/atbs_03_zeroDivide_02_BIS_03.py", line 3, in division1
return 72 / divideBy
ZeroDivisionError: division by zero
that is precisely what I'm trying to avoid by using the try/except block.Thanks again!
All the best,
newbieAuggie2019
"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs