I have two programs I've been working on for my class but I keep getting different errors.

First one:

Write a boolean function called isTriangle that receives three numbers and returns true or false based on whether or not the
numbers are possible lengths of a triangle. If the sum of any two
sides is less than the third side, a triangle cannot be formed.

Here's what I have:

a = raw_input("Length of first side?")
b = raw_input("Length of second side?")
c = raw_input("Length of third side?")

import math

def isTriangle(a, b, c):
if a + b < c:
return 0
elif a + c < b:
return 0
elif b + c < a:
return 0
else:
return 1

print isTriangle

The closest I got, I would get a false even though the numbers formed a triangle.

Second program:

Write a function called heronsFomula that computes the area of a triangle given the length of the three sides. The formula is as
follows: sqrt(s(s-a)(s-b)(s-c)) ,where s is half the perimeter and a, b, and c are the lengths of the sides.

What I put in:

def sides (firstSide, secondSide, thirdSide):

def halfPerimiter(s):

s = (firstSide + secondSide + thirdSide)/2.0

print halfPerimiter

firstSide = raw_input ("Length of first side? ")

firstSide = float (firstSide)

secondSide = raw_input ("Length of second side? ")

secondSide = float (secondSide)

thirdSide = raw_input ("Length of third side? ")

thirdSide = float (thirdSide)

sides (firstSide, secondSide, thirdSide)

I haven't put the formula part in yet because I haven't successfully calculated s.

Thank you.

Wrapping your code in the BB code tags is usually essential (keeps indentation), so remember to do it next time.

First Program
For the first program, `raw_input` returns a string, therefore when comparing the values, you're comparing something like `"3" + "5" < "9"` (i.e. "35" < "9"). This is why it's returning False each time. You need to take the inputs and turn them into integers, so the result would be `3 + 5 < 9` . To do this, you can do:

``````a = int( raw_input("Length of first side?") )
# example:   raw_input returns "9", then int() turns it into 9, and then it's stored in a``````

Plus, `print isTriangle` won't work, it needs to be `print isTriangle( a, b, c )` .
And, finally, you can condense your function by writing:

``````def isTriangle(a, b, c):
if ( a + b < c ) or ( a + c < b ) or ( b + c < a ):
return 0
else:
return 1``````

(As a side note, importing the math module in this program is useless, as no functions or constants from it are being used.)

Second Program
The indentation has been lost due to not using CODE tags. As you (should) know, indentation in Python is essential. One thing I do notice that is odd is the line with `print halfPerimiter` . All this would end up doing is printing the object (pointer/address? I'm not sure of the proper term :\$) halfPerimiter, such as `<function halfPerimiter at 0x01100EB0>` . As for the rest of the second program, it seems fairly illogical to me. I've just re-written the majority of it for you:

``````s1 = float( raw_input("Length of first side? ") )
s2 = float( raw_input("Length of second side? ") )
s3 = float( raw_input("Length of third side? ") )

def heron( s1, s2, s3 ):
hp = ( s1 + s2 + s3 ) / 2.0
# do rest of forumla.``````

There are 4 variables; s1, s2, and s3 store the side lengths, and hp (local to heron) stores half of the perimeter. Now just fill in the rest of the function heron and it should be good.
Hopefully this helped!

just a note shadwickman

for both programs you want to use

``example = float(raw_input("question"))``

because if int is used it will remove all of the floating point numbers to make it an integer

commented: good point +6

Also, add the code for how you call these functions if you post again. Some errors are simply caused by a function not being called with parameters, etc. and since you don't print or return from "sides" there is a potential problem there as well.

Edit: I see that you may or may not be getting the values for the sides outside the function depending on the indents. Do you want to call both functions from there? There is also a problem in what variables you are sending to the functions. halfPerimiter is not correct (other than the spelling of perimeter).

``````def sides (firstSide, secondSide, thirdSide):

def halfPerimiter(s):
s = (firstSide + secondSide + thirdSide)/2.0
print halfPerimiter

firstSide = raw_input ("Length of first side? ")
firstSide = float (firstSide)
secondSide = raw_input ("Length of second side? ")
secondSide = float (secondSide)
thirdSide = raw_input ("Length of third side? ")
thirdSide = float (thirdSide)

sides (firstSide, secondSide, thirdSide)``````

Thanks woooee, I didn't really think about floating point inputs for the first program, just plain old integer input.

Thanks for the help. The first one works but I'm confused with the square root part of the second. I need to take the square root of s (s - a) (s - b) (s - c) but I don't exactly know how to do a square root.

``````s1 = float( raw_input("Length of first side? ") )
s2 = float( raw_input("Length of second side? ") )
s3 = float( raw_input("Length of third side? ") )

def heron( a, b, c):
s = ( a + b + c ) / 2.0

import math

def area (math.sqrt (s(s-a)(s-b)(s-c))

print area``````

There are multiple issues with your code. The first one to catch my attention was

``def area (math.sqrt (s(s-a)(s-b)(s-c))``

Why did you say 'def'? That defines a function, not any other data type. Not only that, but you didn't even close the final bracket pair, you left it hanging unclosed. Lastly (still with this one line), you need to multiply 's' and '(s-a)(s-b)(s-c)', but that requires an asterisk * between them, like `s*(s-a)*(s-b)*(s-c)` . Putting the 's' right against the first parenthesis works in mathematics, but in python, putting parenthesis right after a variable calls the function that variable points to, and 's' is not a function, it's a float. So here's how that one line should read as:

Secondly, the last line is wrong.

``print area``

This will cause an error. Declaring 'area' inside of the function 'heron' means that 'area' is within that function's scope, and can't be accessed from outside of the function. If you want to get the value of area from the function, then return that value, and store it to a variable when you make the call to the 'heron' function.

Finally, you never actually made a call to the 'heron' function. You need to do this and pass the parameters (s1, s2, s3), and store the value it returns (the area). So the line

``print area``

becomes

``area = heron( s1, s2, s3 )``

Here's what the final code should look like:

``````s1 = float( raw_input("Length of first side? ") )
s1 = float( raw_input("Length of first side? ") )
s2 = float( raw_input("Length of second side? ") )
s3 = float( raw_input("Length of third side? ") )

def heron( a, b, c):
s = ( a + b + c ) / 2.0

import math

return math.sqrt( s*(s-a)*(s-b)*(s-c) )

area = heron( s1, s2, s3 )``````

With the above, the user inputs s1, s2, and s3. Then they are passed to the function 'heron' when it is called, and the function returns the value of the area, which is stored as 'area' in the global scope (where you called it from).

I'm giving this to you but PLEASE read the rest of the above and actually understand what it is that you're doing wrong. This is very simple stuff and if you never grasp this then you're not going to ever get anywhere with Python, or really any other language. I hope this helped, and if so, you can finally mark this thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.