Nov-15-2020, 06:18 AM
(This post was last modified: Nov-15-2020, 06:18 AM by KingKhan248.

*Edit Reason: Forgot to add in what happens when code is ran*)
I'm trying to practice using exceptions in python programming but I can't seem to figure out how it works.

I'm trying to create a basic program that asks the user to enter the side lengths of a triangle and then calculates and outputs the perimeter. I want to use exceptions to give error messages when any of the side lengths are entered incorrectly, like if instead of a number they enter "akjdhg", and it should give an error when they enter a negative number.

Still pretty new to this forum I hope I followed all the rules and guidelines on posting here. If not please let me know and I will fix it. Thanks in advance!

I'm trying to create a basic program that asks the user to enter the side lengths of a triangle and then calculates and outputs the perimeter. I want to use exceptions to give error messages when any of the side lengths are entered incorrectly, like if instead of a number they enter "akjdhg", and it should give an error when they enter a negative number.

try: len1 = (float(input("Enter the length of one side of the triangle: "))) len2 = float(input("Enter the length of a different side: ")) len3 = float(input("Enter the length of the final side: ")) except ValueError: print("Please enter a number.") except: if len1 <= 0 or len2 <= 0 or len3 <= 0: print("Please enter a positive number.")When I enter anything other than a number I do get the error message I want but when I enter a negative or 0, the error message does not appear and the code continues.

Still pretty new to this forum I hope I followed all the rules and guidelines on posting here. If not please let me know and I will fix it. Thanks in advance!