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.
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")
_____________________________________________________
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!".
_____________________________________________________
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.
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