Python Forum
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Area of a triangle
#1
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')
Reply
#2
What's some sample input that causes the error?
Reply
#3
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?
Reply
#4
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.
Reply
#5
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...
Reply
#6
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.
Reply
#7
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
Reply
#8
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
Reply
#9
It worked finally!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Sad triangle numbers Woody_MC_2022 5 1,211 Sep-24-2022, 08:14 PM
Last Post: deanhystad
  Fill a value in triangle shape in Matrix lynx 0 1,880 Dec-07-2019, 06:32 AM
Last Post: lynx
  Hollow triangle-drawing characters param error. phob0s 4 2,608 Jul-31-2019, 08:18 AM
Last Post: phob0s
  Printing out a triangle using nested for loops MrGoat 12 6,573 Jan-16-2019, 07:21 PM
Last Post: ichabod801
  Triangle: max path sum Mateoo 1 2,932 Jan-10-2019, 09:16 PM
Last Post: stullis

Forum Jump:

User Panel Messages

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