Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
try/except blocks
#4
(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
Reply


Messages In This Thread
try/except blocks - by newbieAuggie2019 - Oct-05-2019, 02:04 AM
RE: try/except blocks - by wavic - Oct-05-2019, 02:36 AM
RE: try/except blocks - by newbieAuggie2019 - Oct-05-2019, 04:59 AM
RE: try/except blocks - by Larz60+ - Oct-05-2019, 02:57 AM
RE: try/except blocks - by buran - Oct-05-2019, 07:57 AM
RE: try/except blocks - by newbieAuggie2019 - Oct-05-2019, 03:55 PM
RE: try/except blocks - by Gribouillis - Oct-05-2019, 08:39 AM
RE: try/except blocks - by perfringo - Oct-05-2019, 04:42 PM
RE: try/except blocks - by newbieAuggie2019 - Oct-05-2019, 05:55 PM
RE: try/except blocks - by buran - Oct-05-2019, 05:01 PM
RE: try/except blocks - by newbieAuggie2019 - Oct-05-2019, 05:21 PM
RE: try/except blocks - by buran - Oct-05-2019, 05:37 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  RSA Cipher with blocks Paragoon2 0 613 Nov-26-2023, 04:35 PM
Last Post: Paragoon2
  How to properly format rows and columns in excel data from parsed .txt blocks jh67 7 2,154 Dec-12-2022, 08:22 PM
Last Post: jh67
  Am I a retard - else and finally blocks in a try statement RubenF85 6 2,812 Jan-12-2021, 05:56 PM
Last Post: bowlofred
  How to tabulate correctly repeated blocks? Xiesxes 4 3,119 Mar-21-2020, 04:57 PM
Last Post: Xiesxes
  Understanding program blocks newbieAuggie2019 2 2,119 Oct-02-2019, 06:22 PM
Last Post: newbieAuggie2019
  The Empty List Blocks Recursion leoahum 6 5,567 Mar-05-2019, 06:55 PM
Last Post: leoahum

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020