Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python, exceptions
#1
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.

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!
Reply


Messages In This Thread
Python, exceptions - by KingKhan248 - Nov-15-2020, 06:18 AM
RE: Python, exceptions - by ndc85430 - Nov-15-2020, 06:23 AM
RE: Python, exceptions - by KingKhan248 - Nov-15-2020, 06:28 AM
RE: Python, exceptions - by deanhystad - Nov-15-2020, 06:32 AM
RE: Python, exceptions - by buran - Nov-15-2020, 06:37 AM
RE: Python, exceptions - by KingKhan248 - Nov-15-2020, 06:44 AM
RE: Python, exceptions - by buran - Nov-15-2020, 06:54 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  PiCamera - print exceptions? korenron 2 829 Dec-15-2022, 10:48 PM
Last Post: Larz60+
  Class exceptions DPaul 1 1,289 Mar-11-2022, 09:01 AM
Last Post: Gribouillis
  is this a good way to catch exceptions? korenron 14 4,702 Jul-05-2021, 06:20 PM
Last Post: hussaind
  Split string between two different delimiters, with exceptions DreamingInsanity 2 2,013 Aug-24-2020, 08:23 AM
Last Post: DreamingInsanity
  handling 2 exceptions at once Skaperen 2 2,295 Jun-27-2020, 08:55 AM
Last Post: Yoriz
  remove spaces with exceptions catosp 4 2,390 May-29-2020, 09:32 AM
Last Post: catosp
  Looking for advice and Guidance on Exceptions used within Functions paul41 1 2,144 Nov-14-2019, 12:33 AM
Last Post: Larz60+
  multi-line messages in raised exceptions? Skaperen 3 7,300 Aug-01-2019, 02:17 AM
Last Post: Skaperen
  Creating custom exceptions that co-operate LadySvetlana 4 3,069 Mar-19-2019, 04:24 PM
Last Post: LadySvetlana
  Problems with not having exceptions crash my script league55 7 4,836 Feb-16-2018, 09:06 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020