简体   繁体   English

在 python 中使用多个函数

[英]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.我一直在移动 hp 方程,但没有运气。 The area portion has me out of ideas, and I'm not sure where to go from here.区域部分让我没有想法,我不确定从这里到 go 的位置。

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'

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.问题是可变perimeter是指您在上面定义的 function,因此当您执行hp = perimeter / 2行时,您试图将 function 除以 Z157DB7DF530023575515D366 错误说。 If you want to divide the return value of the function instead, you need to call the function:如果你想将 function 的返回值相除,则需要调用 function:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM