Brett
Brett

Reputation: 21

Program to check the area of a triangle if it can or can not be computed based on the sides entered. Python-3.x

This program inputs three integers as side lengths of a triangle and print the area of the triangle. If the sides cannot form a triangle, it prints an appropriate message.

The problems I have include:

Help?

#This program will check to see if the area of the triangle can or can not be computed based on the sides entered.

import math
while True:
     try:
         side1 = int(input("Please enter the value of side 1: "))
         side2 = int(input("Please enter the value of side 2: "))
         side3 = int(input("Please enter the value of side 3: "))
     except ValueError:
         print("Please enter a value next time instead of a letter.")
         continue
     else:
         break

def isValid(side1, side2, side3):
     if (side1 + side2 > side3) or (side1 + side3 > side2) or (side1 + side3 > side2):
         return True
     else:
         return False

def area(side1, side2, side3):
     if isValid(side1, side2, side3) is True:
         sides = (side1 + side2 + side3) / 2
         TriArea = math.sqrt(sides * (sides - side1) * (sides - side2) * (sides - side3))
         print("The area of triangle with sides", side1, side2, side3, "is", TriArea)
     elif isValid(side1, side2, side3) is False:
         print("Please enter numbers that will make a triangle.")

area(side1, side2, side3)

Edit: Upon changing a few things thanks to your responses it now works correctly.

After changing

if (side1 + side2 > side3) or (side1 + side3 > side2) or (side1 + side3 > side2):

to the below allowed the program to run much better.

if (side1 + side2 > side3) and (side1 + side3 > side2) and (side1 + side3 > side2):

Also changing

if isValid(side1, side2, side3) is True:    

to instead the below made it much simpler.

if isValid(side1, side2, side3):

Thank you for the quick responses everyone! :)

Upvotes: 1

Views: 568

Answers (2)

Prune
Prune

Reputation: 77857

The critical problem is your test for the triangle inequality:

 if (side1 + side2 > side3) or (side1 + side3 > side2) or (side1 + side3 > side2):

Your logic is wrong: you need all three of these to be true for a triangle to form, not just one.

 if (side1 + side2 > side3) and (side1 + side3 > side2) and (side2 + side3 > side1):
     # Note the change in the last clause: you made one test twice, and skipped the third.

will fix most of your troubles.

Additionally, please get comfortable with Booleans as values. First, the above routine can read simply

 return (side1 + side2 > side3) or (side1 + side3 > side2) or (side1 + side3 > side2)

Second, your call to this routine is over-done: call it once and check the value once:

 if isValid(side1, side2, side3):
     # Compute area
 else
     # Print error message

You don't need to compare the result against True: it already has a logic value. Second, if you get to the else part, you are guaranteed that the function returned False -- no need to call a second time with the same data; no need to check against False.


AFTER OP EDIT:

if isValid(side1, side2, side3) == True:

should be simply

if isValid(side1, side2, side3):

Do not compare a Boolean value to a Boolean constant; this is a redundant check. You already have a logic value returned from the function.

Upvotes: 2

Nick T
Nick T

Reputation: 26717

Your code has many problems:

  1. The logic of your isValid function is wrong. If any of those predicates separated by or's is true, it will return True. So if sides are 1, 1, 1000, it gives True...you should probably return False there.

    As an aside, if you sorted the side lengths, you could also get away with a single comparison.

  2. You're using is to check equality. is checks object identity, not equality. Do not do that. Instead:

    if isValid(...):
        ...
    else: # don't repeat isValid, it's a waste
        ...
    

Upvotes: 2

Related Questions