Python 类型错误:';str';对象不可调用

Python 类型错误:';str';对象不可调用,python,string,while-loop,Python,String,While Loop,当我回答时,这种情况就会发生 首先是一个空行,然后如果我再次按enter键 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()("

当我回答时,这种情况就会发生

首先是一个空行,然后如果我再次按enter键

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.")
文件“test.py”,第11行,在
ans=raw_input().lower()(“这是只有是/否才会出现的情况之一
“做!”“\n”“那会是什么?是?否?”)
TypeError:“str”对象不可调用
有什么问题


另外,我搜索了这个网站,但发现所有有同样问题的人都有更高级的脚本,我什么都不懂

您需要执行
raw\u输入(“这是只有Yes/No才能执行的情况之一!”“\n”“那么会是什么?Yes?No?”)。lower()


当您执行
raw\u input().lower()
时,它已调用
raw\u input()
,并将结果转换为小写。到那时,尝试传递提示字符串已经太晚了。

TypeError:“str”对象是不可调用的
通常意味着您正在字符串上使用
()
符号,Python尝试将该
str
对象用作函数。e、 g.
“你好世界”(
)或
“你好”(“世界”)

我想你的意思是:

  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
另一个错误:

ans = raw_input("This is one of those times...").lower()
您必须分别检查这两种情况

应该是:

if ans() == 'yes' or 'no':
或者更符合您最初的需求:

 if ans == 'yes' or ans == 'no':
        break

第一个错误在这行中

if ans in ('yes', 'no')
lower()
的结果是一个字符串,其后的括号表示调用左侧的对象(字符串)。因此,你得到了你的错误。你想要

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
是一个字符串,括号表示调用左侧的对象(字符串)。因此,你得到了你的错误

另外,
是一个逻辑运算符。即使在删除
ans
后面的括号后,代码的计算结果仍为:

if ans() == 'yes' or 'no':
由于非空字符串(
'no'
)的计算值为True,因此此表达式始终为True。你只是想要

if (ans == 'yes') or ('no'):
此外,您希望取消最后一行的缩进。总之,请尝试:

if ans in ('yes', 'no'):

这帮我跑了一圈。如果我再次回答“dhfjdshfkjdsj”,程序将崩溃并返回:回溯(最近一次调用):文件“test.py”,第13行,在If ans()='yes'或'no':TypeError:'str'对象不存在callable@AdrianLarsson:这是一个独立但相似的问题。更多细节请参见phihag的答案。(但我认为你应该接受布伦巴恩的回答,因为他简洁地解决了你原来的问题,而且比任何人都早。)更新:是的,我都做到了,如果我不止一次地写垃圾,我仍然坚持。哇!真管用!我现在可以永远运行循环,不管我写的是什么垃圾。然而;当我试图回答“是”或“否”时,它仍然会回答“是”;“这是其中一个只有“是/否”才行的时刻!”在最后一次编辑之后,这解释了我答案中的所有内容,因此我将删除我的答案。但是格式是错误的(在“总之,试试:”代码不是代码块之后);仍然让我永远陷入while循环。即使我马上回答“是”。@AdrianLarsson更新了一个完整的解决方案。另外,你可以在ans不在('yes','no')时执行
而不是
,而True:
后面紧跟着等价的
if
语句和
break
if ans in ('yes', 'no'):
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.")