Python Forum

Full Version: Area of a triangle
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
First of all I need to verify if I can draw a triangle, is_valid(s1,s2,s3): is giving correct answers.
Then, I have to find the area of the triangle, the answers are also correct.
Finally, when I have to find if the triangle is valid AND find the area of the triangle, it's giving me correct answers for if the triangle is valid, and giving me a ValueError: math domain something for when the values give me an invalid triangle.

Help?

import sys
import math
s1 = float(sys.argv[1])
s2 = float(sys.argv[2])
s3 = float(sys.argv[3])

def is_valid(s1, s2, s3):
	lf=[float(s1), float(s2), float(s3)]
	for flt in lf:
		if s1 + s2 > s3 and s1 + s3 > s2 and s2 + s3 > s1:
			return True
		else:
			return False
			
print(is_valid(s1, s2, s3))

def area(s1, s2, s3):
	s=(s1 + s2 + s3)/2
	ar=math.sqrt(s*(s-s1)*(s-s2)*(s-s3))
	return ar
	
print(area(s1, s2, s3))

if is_valid(s1, s2, s3) == True:
	print('Valid Triangle:', end='')
	print(area(s1, s2, s3))
else:
	print('Invalid Triangle')
What's some sample input that causes the error?
it works fine for me.
 
Output:
$ python3 triangle_test.py 10 15 20 True 72.61843774138907 Valid Triangle:72.61843774138907
can you provide specific values for s1-s3 as well the full traceback you get?
c:\Users\OmarHS\Desktop\AUB\CMPS\200\ASST3>python triangle_asst3.py 2.7 5.2 1.3
False
Traceback (most recent call last):
  File "triangle_asst3.py", line 22, in <module>
    print(area(s1, s2, s3))
  File "triangle_asst3.py", line 19, in area
    ar=math.sqrt(s*(s-s1)*(s-s2)*(s-s3))
ValueError: math domain error

(Sep-25-2017, 06:53 PM)buran Wrote: [ -> ]it works fine for me.
 
Output:
$ python3 triangle_test.py 10 15 20 True 72.61843774138907 Valid Triangle:72.61843774138907
can you provide specific values for s1-s3 as well the full traceback you get?

Yes it does work for a valid triangle, but doesn't work for an invalid one in the last part of the code.
As both me and nilamo asked - provide specific set of s1-s3 that cause the error. for me it works
Output:
$ python3 pinfo.py 10 15 5 False 0.0 Invalid Triangle
probably you try to calculate square root from negative number and then it cause an error? :-) You may want to use try/except to catch the ValuError... Try for your self and if you don't succeed - ask again...
In your area function, if you put in the following, you'll see that those inputs produce a negative number.  Which doesn't make sense to pass to math.sqrt, since there exists no number, squared, which results in a negative.
    val = s * (s - s1) * (s - s2) * (s - s3)
    print(val)
    ar = math.sqrt(val)
The simple solution, is to use abs so you always pass a positive value to math.sqrt.  Or, you can use a fractional exponent to get the same(ish) result ar = val ** 0.5.
when s1 = 2.7
s2 = 5.2
s3 = 1.3

It's giving me the ValueError mentioned before, for 10 15 and 5 it works

Tried adding the (abs) still not working for THESE specific values
As I said - use try/except
def area(s1, s2, s3):
    s=(s1 + s2 + s3)/2
    try:
        ar=math.sqrt(s*(s-s1)*(s-s2)*(s-s3))
        return ar
    except ValueError:
        return 0.0
It worked finally!