Exception handling using Python's ZeroDivisionError

Introduction

This time, the university report said, "Create some exception handling in Python!", So it's easy, but I created exception handling. I hope it will be helpful for those who are stuck with exception handling.

Code created

try_except.py


#Encourage you to want to divide
print("\n_____________________________________________________\n")
print("Currently, you have a desire to divide.")
print("Divide as much as you want here.")
print("After entering a number, press Enter and enter the next number.")
print("\n * By the way, you should never put "0 (zero)" in y *")
print("_____________________________________________________\n")

#Input and output numbers
print("x = ",end="")
x = float(input())
print("y = ",end="")
y = float(input())
print("\n" + str(x) + " ÷ " + str(y))

#Exception handling
try:
    z = 0
    z = x / y
    print("= " + str(z))
    print("Correct!\n")
except ZeroDivisionError as e:
    print("= \nYou don't deserve an answer.")
    print("Don't come here again.")
    print("\n Ah ... you put 0 in y, right?\n")

Execution result (when a number other than 0 is entered in y)

_____________________________________________________

Currently, you have a desire to divide.
Divide as much as you want here.
After entering a number, press Enter and enter the next number.

* By the way, you should never put "0 (zero)" in y *
_____________________________________________________

x = 2
y = 1

2.0 ÷ 1.0
= 2.0
Correct!

You can calculate normally and you will see "Correct!".

Execution result (when 0 is entered in y)

_____________________________________________________

Currently, you have a desire to divide.
Divide as much as you want here.
After entering a number, press Enter and enter the next number.

* By the way, you should never put "0 (zero)" in y *
_____________________________________________________

x = 2
y = 0

2.0 ÷ 0.0
= 
You don't deserve an answer.
Don't come here again.

Ah ... you put 0 in y, right?

If you put 0 in y, you will get very angry.

in conclusion

This time I wrote a simple exception handling code in Python3. I think there are many places that can be added to this code, so if you are interested, please strengthen it. See you again.

Recommended Posts

Exception handling using Python's ZeroDivisionError
Exception handling
Python exception handling
boto3 exception handling
Python exception handling
Python, about exception handling
Try using Python's feedparser.
Try using Python's Tkinter
Fizzbuzz with exception handling
Python exception handling (Python learning memo ⑥)
Send email using Python's smtplib
Error divided by 0 Handling of ZeroDivisionError
Exception handling during Python API communication
Generate QR code using Python's "qrcode"
Learn about logging using Python's logging module ①
A story about using Python's reduce
Try using Python's networkx with AtCoder