Python 类型错误:“str”对象不可调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14131780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: 'str' object is not callable
提问by LoneDruidOfTheDarkArts
name = raw_input("Welcome soldier. What is your name? ")
print('Ok,', name, ' we need your help.')
print("Do you want to help us? (Yes/No) ")
ans = raw_input().lower()
while True:
ans = raw_input().lower()("This is one of those times when only Yes/No will do!" "\n" "So what will it be? Yes? No?")
ans = raw_input().lower()
if ans() == 'yes' or 'no':
break
if ans == "yes":
print ("Good!")
elif ans == "no":
print("I guess I was wrong about you..." '\n' "Game over.")
When I answer this happens;
当我回答时会发生这种情况;
First a blank line, then if I press the enter key again;
首先是一个空行,然后如果我再次按下回车键;
File "test.py", line 11, in <module>
ans = raw_input().lower()("This is one of these times when only Yes/No will
do!" "\n" "So what will it be? Yes? No?")
TypeError: 'str' object is not callable
What seams to be the problem?
什么接缝是问题?
P.S I searched the site but it seams that all the people with the same problem had far more advanced scripts and I did not understand anything.
PS 我搜索了该网站,但发现所有遇到相同问题的人都有更高级的脚本,而我什么也不懂。
采纳答案by phihag
The first error is in the line
第一个错误在行中
ans = raw_input().lower()("This is one of those times when only Yes/No will do!"
"\n" "So what will it be? Yes? No?")
The result of lower()is a string, and parentheses after that mean that the object on the left (the string) gets called. Therefore, you get your error. You want
的结果lower()是一个字符串,后面的括号表示左边的对象(字符串)被调用。因此,你会得到你的错误。你要
ans = raw_input("This is one of those times when only Yes/No will do!\n"
"So what will it be? Yes? No?").lower()
Also,
还,
if ans() == 'yes' or 'no':
does not what you expect. Again, ansis a string, and parentheses mean that the object on the left (the string) gets called. Therefore, you get your error.
不是你所期望的。同样,ans是一个字符串,括号表示左边的对象(字符串)被调用。因此,你会得到你的错误。
Also, oris a logical operator. Even after removing the parentheses after ans, the code gets evaluated as:
此外,or是一个逻辑运算符。即使在删除之后的括号之后ans,代码也会被评估为:
if (ans == 'yes') or ('no'):
Since a non-empty string ('no') evaluates to the booleanvalue True, this expression is always True. You simply want
由于非空字符串 ( 'no') 的计算结果为布尔值 True,因此该表达式始终为 True。你只是想要
if ans in ('yes', 'no'):
Additionally, you want to unindent the last lines. All in all, try:
此外,您希望取消缩进最后几行。总而言之,尝试:
name = raw_input("Welcome soldier. What is your name? ")
print('Ok, ' + name + ' we need your help.')
ans = raw_input("Do you want to help us? (Yes/No)").lower()
while True:
if ans in ('yes', 'no'):
break
print("This is one of those times when only Yes/No will do!\n")
ans = raw_input("So what will it be? Yes? No?").lower()
if ans == "yes":
print("Good!")
elif ans == "no":
print("I guess I was wrong about you..." '\n' "Game over.")
回答by BrenBarn
You need to do raw_input("This is one of those times when only Yes/No will do!" "\n" "So what will it be? Yes? No?").lower().
你需要做raw_input("This is one of those times when only Yes/No will do!" "\n" "So what will it be? Yes? No?").lower()。
When you do raw_input().lower(), that already calls raw_input()and converts the result to lowercase. By that time it's too late to try to pass your prompt string.
当你这样做时raw_input().lower(),它已经调用raw_input()并将结果转换为小写。到那时尝试传递提示字符串为时已晚。
回答by bakkal
TypeError: 'str' object is not callableusually means you are using your ()notation on a string, and Python tries to use that strobject as a function. e.g. "hello world"(), or "hello"("world")
TypeError: 'str' object is not callable通常意味着您()在字符串上使用您的符号,而 Python 尝试将该str对象用作函数。例如"hello world"(),或"hello"("world")
I think you meant to do :
我想你的意思是:
ans = raw_input("This is one of those times...").lower()
Another mistake:
另一个错误:
if ans() == 'yes' or 'no':
You have to check for both conditions individually,
您必须分别检查这两个条件,
Should be :
应该 :
if ans == 'yes' or ans == 'no':
break
or more inline with what you wanted in the first place:
或者更符合您最初想要的:
if ans in ('yes', 'no')

