Shawnphi
Shawnphi

Reputation: 35

Using multiple functions in python

I am having trouble trying to input an area and parameter of a triangle using multiple functions. I just started learning defining functions and can't figure this out. The perimeter works just fine. I kept moving the hp equation around, but no luck. The area portion has me out of ideas, and I'm not sure where to go from here.

def perimeter(a,b,c):
    return a + b + c

def area(hp, a, b, c):
    return (hp * (hp - a) * (hp - b) * (hp - c)) ** (1/2)

def main():
    a = eval(input("Enter side 'a': "))
    b = eval(input("Enter side 'b': "))
    c = eval(input("Enter side 'c': "))

    hp = perimeter / 2
    per = perimeter(a,b,c)
    areaTri = area(hp,a,b,c)

    print("\n",per)
    print(areaTri)

main()
Error message: line 22, in main
    hp = perimeter / 2
TypeError: unsupported operand type(s) for /: 'function' and 'int'

Upvotes: 0

Views: 64

Answers (1)

awarrier99
awarrier99

Reputation: 3855

The issue is the variable perimeter refers to the function you defined above, so when you do the line hp = perimeter / 2 you're trying to divide a function by an integer, as the error says. If you want to divide the return value of the function instead, you need to call the function:

per = perimeter(a,b,c)
hp = per / 2

Upvotes: 3

Related Questions