Python: Exception Handling

discount = 20

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

try:
    calculated_price = float(price) - discount
except:
    calculated_price = 0

print(calculated_price)
Python

Run the script again and enter some string when it asks for the price-

Enter price of the product: abcdef
0
Python

Run the script and enter some decimal value when it asks for the price-

Enter price of the product: 33
13.0
Python

Leave a Comment


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