简体   繁体   English

类型错误:“str”对象在输入行上不可调用

[英]TypeError: 'str' object is not callable on a input line

I'm a Python beginner looking for some help with this piece of code:我是一名 Python 初学者,正在寻找有关这段代码的帮助:

elif exit is False and temp is ('1'):
add_more = True
add_1 = 'y'
while add_more is True:
    races.append(input('What is the name of the race: '))
    print('Racelist is now: ' + str(races))
    add_1 (input('Want to add more? yn '))
    if add_1 is ('y'):
        add_more = True
    else:
        addmore = False

When I run it I keep getting the following error:当我运行它时,我不断收到以下错误:

Traceback (most recent call last):
  File "C:/Users/David/PycharmProjects/Eclipse/Eclipse.py", line 43, in <module>
    add_1 (input('Want to add more? yn '))
TypeError: 'str' object is not callable

When I comment out the affected line the loop works and keeps asking: races.append(input('What is the name of the race: ')) (the variable races is a list).当我注释掉受影响的行时,循环工作并不断询问: races.append(input('What is the name of the race: ')) (变量races是一个列表)。

I've been breaking my neck on it all night and google was no help so far.我整晚都在折腾我的脖子,到目前为止谷歌没有帮助。 I cannot understand why the line does not work but the other input does work.我不明白为什么这条线不起作用,但其他输入却起作用。 . .

It seems there are a few things wrong in your code.您的代码中似乎有一些错误。 In the else statement you set addmore = False , i assume this is meant to be add_more = False .在您设置addmore = False的 else 语句中,我假设这是add_more = False Also i assume that you want to allocate the user response to the variable add_1.此外,我假设您想将用户响应分配给变量 add_1。 Lastly you should not use is to compare values.最后,您不应该使用 is 来比较值。 Instead use the == or != type comparisons.而是使用==!=类型比较。 You could try the following你可以试试下面的

add_more = True
add_again = 'y'
while add_more:
    races.append(input('What is the name of the race: '))
    print('Racelist is now: ' + str(races))
    add_again = input('Want to add more? yn ')
    if add_again.lower() != 'y':
        add_more = False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM