I keep getting TypeError: 'str' object is not callable and I am not sure how to fix it

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() 函数将值打印到 shell 或控制台,但没有返回任何内容。这就是为什么当您将 print() which returns NoneType 与字符串格式的 str() 函数连接时会导致错误的原因。 Python 不允许对不同类型的变量进行连接操作。这是您应该更正代码的内容。

PI = 3.141593
radius_p = float(input('radius'))
perimeter = 2 * PI * radius_p
print('perimeter:', str(perimeter))

您应该通过输入 4 得到如下所示的输出。

注意:如果问题仍然存在,那么您需要与我们分享完整的代码,因为您可能正在使用 str 对象作为变量名 某处不正确。

打印语句行的语法错误。应该是

print('perimeter: ' + str(perimeter))

如果您仍然遇到错误,只是为了检查将 perimeter 的字符串值分配给另一个变量并打印

newperi = str(perimeter)

print('perimeter: ' + newperi)