python math domain error - sqrt -


what causes problem?

from math import sqrt print "a : " = float(raw_input()) print "b : " b = float(raw_input()) print "c : " c = float(raw_input()) d = (a + b + c)/2 s = sqrt(d*(d-a)*(d-b)*(d-c)) print "a+b+c =", a, b, c print "distr. =", d*2, "area =", s 

error:

traceback (most recent call last):    file "c:/python27/fájlok/háromszög terület2.py", line 11, in <module>        s = sqrt(d*(d-a)*(d-b)*(d-c)) valueerror: math domain error 

the problem heron's formula holds when sum of 2 numbers greater third. need check explicitly.

a better way using code using exception handling

try:     s = sqrt(d*(d-a)*(d-b)*(d-c))     print "a+b+c =", a, b, c     print "distr. =", d*2, "area =", s except valueerror:     print "please enter 3 valid sides" 

if want without try block can as

delta = (d*(d-a)*(d-b)*(d-c)) if delta>0:     s = sqrt(delta)     print "a+b+c =", a, b, c     print "distr. =", d*2, "area =", s else:     print "please enter 3 valid sides" 

Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -