[英]I keep getting TypeError: 'str' object is not callable (python 2.7)
[英]I keep getting TypeError: 'str' object is not callable and I am not sure how to fix it
这是我写的代码,它应该告诉我圆的半径,但我一直收到错误。
PI = 3.141593
radius_p = float(input('radius'))
perimeter = 2 * PI * radius_p
print('perimeter: ') + str(perimeter)
# Ask the user for the radius of the room
# Calculate the perimeter
# Print the perimeter
this is what I get after
```
TypeError Traceback (most recent call last)
<ipython-input-80-ba1678e7ffca> in <module>()
2 radius_p = float(input('radius'))
3 perimeter = 2 * PI * radius_p
----> 4 print('perimeter: ') + str(perimeter)
5 # Ask the user for the radius of the room
6 # Calculate the perimeter
TypeError: 'str' object is not callable
问题是print()
function 将值打印到 shell 或控制台而不返回任何内容。 这就是为什么当您将返回NoneType
的print()
与字符串格式的str()
function 连接时,会导致错误。 Python 不允许对不同类型的变量进行 concat 操作。 这是您应该更正代码的内容。
PI = 3.141593
radius_p = float(input('radius'))
perimeter = 2 * PI * radius_p
print('perimeter:', str(perimeter))
你应该得到一个 output 就像我在下面通过输入 4 所做的那样。
注意:如果问题仍然存在,那么您需要与我们共享完整代码,因为您可能在某处使用 str object 作为不正确的变量名。
打印语句行的语法错误。 它应该是
print('周长:' + str(周长))
如果您仍然收到错误,仅用于检查将周长的字符串值分配给另一个变量并打印
newperi = str(周长)
print('周长:' + newperi)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.