0

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'
5
  • hp = perimeter((a,b,c)) / 2 Commented May 1, 2020 at 23:09
  • 3
    hp = perimeter(a,b,c) / 2 Commented May 1, 2020 at 23:10
  • 3
    @pinkspikyhairman the double parentheses will cause it to interpret the argument as a tuple, rather than three separate arguments Commented May 1, 2020 at 23:11
  • 1
    thanks everyone, much appreciated Commented May 1, 2020 at 23:12
  • 1
    Don't use eval() for this! You likely just need int(). Commented May 1, 2020 at 23:24

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.