Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python, exceptions
#5
Looking at your code I think you misunderstand how the try/except works. When there is error in the try block it looks at the exceptions. You handle ValueError and you also have all-catching blank except`. When you enter a negative number, no exception is raised so no except block is entered.

Probably you want to have else part. It will run after the try block if no except` block was entered
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.")
else:
    if len1 <= 0 or len2 <= 0 or len3 <= 0:
     
        print("Please enter a positive number.")
Note that it will print the message and just continue with program execution.
For completeness you should know there can be also finally - i.e. this code will be executed always, regardless of except part being executed or not.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

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 857 Dec-15-2022, 10:48 PM
Last Post: Larz60+
  Class exceptions DPaul 1 1,316 Mar-11-2022, 09:01 AM
Last Post: Gribouillis
  is this a good way to catch exceptions? korenron 14 4,758 Jul-05-2021, 06:20 PM
Last Post: hussaind
  Split string between two different delimiters, with exceptions DreamingInsanity 2 2,056 Aug-24-2020, 08:23 AM
Last Post: DreamingInsanity
  handling 2 exceptions at once Skaperen 2 2,335 Jun-27-2020, 08:55 AM
Last Post: Yoriz
  remove spaces with exceptions catosp 4 2,448 May-29-2020, 09:32 AM
Last Post: catosp
  Looking for advice and Guidance on Exceptions used within Functions paul41 1 2,182 Nov-14-2019, 12:33 AM
Last Post: Larz60+
  multi-line messages in raised exceptions? Skaperen 3 7,404 Aug-01-2019, 02:17 AM
Last Post: Skaperen
  Creating custom exceptions that co-operate LadySvetlana 4 3,118 Mar-19-2019, 04:24 PM
Last Post: LadySvetlana
  Problems with not having exceptions crash my script league55 7 4,932 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