[英]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
When you did: 当您这样做时:
raw_input = ("?")
You assigned the name raw_input
to the string "?"
您
raw_input
名称raw_input
分配给字符串"?"
, 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). 为变量使用一个不同的名称(尽管看起来您甚至没有使用它,所以您只需删除有问题的行)。
You masked the raw_input
function with raw_input = ("?")
. 您用
raw_input = ("?")
掩盖了raw_input
函数。 You're looking for user_input = raw_input("?")
, as you did correctly with repeat3
. 您正在寻找
user_input = raw_input("?")
,就像您正确使用了repeat3
。
>>> 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. 您在代码中所做的就是重写了
raw_input
实际含义。 instead of keeping it as a function that gets user input, you turned raw_input
into a regular variable that contains a '?'
而不是将其保留为获取用户输入的函数,而是将
raw_input
转换为包含'?'
的常规变量'?'
( 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. 您将得到相同的错误,因为它实际上是您要尝试执行的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.