简体   繁体   English

Python:TypeError:“ str”对象无法通过if语句调用

[英]Python: TypeError: 'str' object is not callable with if statement

I've read over 50 posts with similar 'title' and have yet to answer my question. 我已经阅读了50多个具有类似“标题”的帖子,但尚未回答我的问题。

If this question is a duplicate please link the [SOLVED] post. 如果该问题重复,请链接[已解决]帖子。 Using Python 2.7.7 使用Python 2.7.7

count = 0
playerChoice = str(raw_input("What do you do this turn? "))

if playerChoice() == 'r':
    count = count + 1
    print "Added one bullet. ", count

Error it gives is: 它给出的错误是:

if playerChoice() == 'r':
TypeError: 'str' object is not callable

To my understanding my code does this: 据我了解,我的代码做到了:

If the player enters str('r'). 如果玩家输入str('r')。 Count will add one and the print statement should print: "Added one bullet." Count将添加一个,并且打印语句应打印:“添加了一个项目符号”。 However, when you enter 'r' in the raw_input. 但是,当您在raw_input中输入“ r”时。 It gives an error. 它给出了一个错误。 Why is that? 这是为什么? I tried adding %s to the raw_input (I read it on another post), however that did not do a thing. 我尝试将%s添加到raw_input(我在另一篇文章中阅读了),但是那没做。

Help would be appreciated thank you. 帮助将不胜感激,谢谢。

playerChoice will evaluate to a string. playerChoice将计算为字符串。 So calling it later with playerChoice() , ( adding the () at the end calls the variable ), throws an error because a string cant be called like a function can. 因此,稍后使用playerChoice()调用它()在末尾添加()调用该变量)会抛出错误,因为无法像函数一样调用字符串。

Use playerChoice to evaluate the string instead: 使用playerChoice评估字符串:

playerChoice = raw_input("What do you do this turn? ")

if playerChoice == 'r':
    count += 1
    print "Added one bullet. ", count

if playerChoice() == 'r': should be if playerChoice == 'r': minus the () if playerChoice() == 'r':应该是if playerChoice == 'r':减去()

You don't call a string, it is not a function. 您不调用字符串,它不是函数。

Also raw_input("What do you do this turn? ") is already a string no need to cast 另外raw_input("What do you do this turn? ")已经是一个字符串,不需要强制转换

If you wanted an int : 如果您想要一个int

you would have to cast int(raw_input("What do you do this turn? ")) but not for strings. 您将必须int(raw_input("What do you do this turn? "))int(raw_input("What do you do this turn? "))而不是字符串。

playerChoice is a variable, not a function. playerChoice是变量,而不是函数。 Use: 采用:

if playerChoice == 'r':

rather than playerChoice() . 而不是playerChoice()

Since playerChoice is a variable and not a reference to a function call, you will not need parentheses to use playerChoice. 由于playerChoice是变量,而不是对函数调用的引用,因此使用playerChoice不需要括号。 If you use playerChoice() , interpreter will try to invoke a method, and will get failed because playerChoice is not referring to a function call. 如果使用playerChoice() ,则解释器将尝试调用方法,但由于playerChoice未引用函数调用而失败。

Thing here to understand is that str(raw_input("What do you do this turn? ")) returns a string, not a function. 这里要了解的是str(raw_input("What do you do this turn? "))返回一个字符串,而不是一个函数。 therefore you will not need to use parentheses. 因此,您将不需要使用括号。

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

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