gpt4 book ai didi

python - 为什么我从 "TypeError: ' 之类的代码中得到 "5(side_length**2)"int' object is not callable"?

转载 作者:太空宇宙 更新时间:2023-11-04 00:45:35 25 4
gpt4 key购买 nike

我有这个代码:

import math

length_centre = float(input("Enter length from centre to corner of pentagon: "))
side_length = 2*length_centre*(math.sin(math.pi/5))
print(side_length)

areaP = (5((side_length)**2))/(4*((math.tan)((math.pi)/5)))

我在最后一行收到一个错误,上面写着 TypeError: 'int' object is not callable。为什么?我该如何解决?

最佳答案

编程语言没有像书面数学那样的隐式乘法,所以 5((side_length)**2) 是不合法的,它试图调用 5 作为带有参数 side_length ** 2 的函数。我猜你想要 5 * side_length**2(删除一些不需要的无关括号,因为无论如何求幂比其他数学操作绑定(bind)得更紧密)。

将其全部清理干净,您将拥有:

import math

# Use float to get the specific type you want, rather than executing whatever
# code the user types. If this is Py2, change input to raw_input too,
# because Py2's input is equivalent to wrapping raw_input in eval already
length_centre=float(input("Enter length from centre to corner of pentagon: "))

# Strip a whole bunch of unnecessary parens
side_length = 2 * length_centre * math.sin(math.pi / 5)
print(side_length)


# Strip a whole bunch of unnecessary parens (left in some technically unnecessary parens
# because they group the complex operands for the division visually)
areaP = (5 * side_length**2) / (4 * math.tan(math.pi / 5))
print(areaP)

关于python - 为什么我从 "TypeError: ' 之类的代码中得到 "5(side_length**2)"int' object is not callable"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39800654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com