Python: Exceptions

First, check the following code-

a = 101 / 0
Python

Run this code-

Output:

raceback (most recent call last):
  File "example.py", line 1, in <module>
    a = 101 / 0
        ~~~~^~~
ZeroDivisionError: division by zero
Plaintext

What just happened?

Or check the following code example-

discount = 20

price = input("Enter price of the product: ")

calculated_price = float(price) - discount

print(calculated_price)
Python

Run the program, and enter 100-

Enter price of the product: 100
80.0
Plaintext

Now run the program again and enter some string as input. Here we have entered “some string here”-

Enter price of the product: some string here
Traceback (most recent call last):
  File "bigboxexample.py", line 5, in <module>
    calculated_price = float(price) - discount
                       ^^^^^^^^^^^^
ValueError: could not convert string to float: 'some string here'
Plaintext

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.