Why Do I Get "Typeerror: 'Str' Object Is Not Callable" with This Code

Why does code like `str = str(...)` cause a TypeError, but only the second time?

Where the code says:

global str
str = str(parameter)

You are redefining what str() means. str is the built-in Python name of the string type, and you don't want to change it.

Use a different name for the local variable, and remove the global statement.

Note that if you used code like this at the Python REPL, then the assignment to the global str will persist until you do something about it. You can restart the interpreter, or del str. The latter works because str is not actually a defined global variable by default - instead, it's normally found in a fallback (the builtins standard library module, which is specially imported at startup and given the global name __builtins__).

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

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.
Sample Image

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.

Why do i get str object not callable error

The problem is in the line 6 actually, where you try to assign an value to the print:

print = ('Hi, ' + name + '. I am thinking of a number between -99 and 99.')

You should delete that = and the code will work :)

import random

print('Hello! What is your name?')
name = input()

print('Hi, ' + name + '. I am thinking of a number between -99 and 99.')
secretNumber = random.randint(-99,99)

for guessesTaken in range(1,7):
print ('Pick a number.')
guess = int(input())

if guess < secretNumber:
print ('Your guess is too low. Guess again.')
elif guess > secretNumber:
print('Your guess is too high. Guess again.')
else:
break # This means the guess was correct

if guess == secretNumber:
print ('Good job, ' + name + '. You guessed the value in ' + str(guessesTaken) + ' guesses.')
else:
print ('You did not guess the number. The correct number was ' + str(secretNumber) + '.')

Python: how to solve TypeError: 'str' object is not callable

ty removing ():

@app.route('/request', methods=["POST"])
def reqpost():
content = request.get_json()
h = content['type']
if h == 'one':
typeone()
return ("running type one")
elif h == 'two':
typetwo()
return ("running type two")
else:
return ("error!")

Why do I get 'str' object is not callable?

I would guess that you defined somewhere in your notebook something like plt.xlabel = "something". This could also happened before you run this code shown. Try to close the Notebook and restart your Kernel. After restarting run your code shown and everything should be fine.

range() function produces error 'TypeError: 'str' object is not callable'

Is it possible you've shadowed range or len by accident by assigning a string to a variable with either name? You should avoid using any of the built-ins as object names.



Related Topics



Leave a reply



Submit