pythonstringhelper

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


This is the code I have written and it should tell me the radius of the circle after but instead I keep getting an error.

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

Solution

  • The issue is that the print() function prints a value to the shell or the console without returning anything. This is why when you are concatenating print() which returns NoneType with a str() function which is in string format, it results in an error. Python doesn't allow concat operation with variables of different types. This is what you should correct your code to.

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

    You should get an output like the one I did below by entering 4. enter image description here

    Note: If the problem persists then you need to share the full code with us because you might be using the str object as a variable name somewhere which is incorrect.