Sadie
Sadie

Reputation: 63

TypeError: 'str' object is not callable

I am trying to run the simple code below:

from sys import argv
script, filename = argv
print ("erase %r") % filename
raw_input = ("?")
target = open(filename, 'w')
print ("truncate")
target.truncate()
print ("fline")
repeat3 = raw_input ('> ')
print ("write")
target.write(repeat3)

I keep getting the error:

Traceback (most recent call last):
  File "ex9.py", line 9, in <module>
    repeat3 = raw_input ('> ')
TypeError: 'str' object is not callable

Upvotes: 1

Views: 538

Answers (3)

TehTris
TehTris

Reputation: 3217

>>> type(raw_input)
<type 'builtin_function_or_method'>
>>> raw_input = '?'
>>> type(raw_input)
<type 'str'>

What you did in your code was you overwrote what raw_input actually means. instead of keeping it as a function that gets user input, you turned raw_input into a regular variable that contains a '?' ( a string)

and Strings are not callable.

For example, if you run:

"?"('>')

You would get the same error, because its literally what you were trying to do.

Upvotes: 1

TigerhawkT3
TigerhawkT3

Reputation: 49318

You masked the raw_input function with raw_input = ("?"). You're looking for user_input = raw_input("?"), as you did correctly with repeat3.

Upvotes: 1

Thomas Orozco
Thomas Orozco

Reputation: 55197

When you did:

raw_input = ("?")

You assigned the name raw_input to the string "?", so you can no longer call the function (instead, you're calling the string, which doesn't work).

Use a different name for your variable (though it looks like you're not even using it, so you could simply remove the offending line).

Upvotes: 5

Related Questions