简体   繁体   中英

TypeError: 'str' object is not callable

I'm very new to Python and I was wondering if anyone could help me with this code I wrote:

print("What is your name:")
name = raw_input()
print "hi", name, "How are you:"
Feeling = raw_input()
if Feeling ("good"):
   print ("Good")
Else
print ("Thats a shame")

It just comes up with error this when I run it:

TypeError: 'str' object is not callable

I have looked online but I couldn't find anything that would help because the answers were to complex.

Could someone please help me.

You are trying to use the string stored in Feeling as a function.

if Feeling ("good"):

With nothing but spaces between Feeling and the parenthesis around "good" , Python sees that as a function call.

You probably wanted to test if it was equal to "good" instead:

if Feeling == "good":
     print "Good"

In this line:

if Feeling ("good"):

You're trying to call Feeling as a function, with "good" as an argument. That's what parentheses mean in this syntax.

If you want to check whether Feeling is equal to "good" , you use the == operator:

if Feeling == "good":

Meanwhile, as soon as you fix that, you're going to have another problem with the next line:

Else

In Python, capitalization counts; else has to be spelled in all lowercase. Also, block statements (like if and else ) always need colons. And the statements inside a block statement have to be indented. So, you want this:

else:
    print("Thats a shame")

You may want to find a smarter editor that helps you with some of this stuff, instead of using Notepad/TextEdit/etc. Any decent programmer's editor—including free ones like SciTE or emacs—will try to put the cursor at the right place for indentation, highlight when you've made a syntax error, and so on.

The problem is this line:

if Feeling ("good"):

Feeling is a string, the result of raw_input(). Feeling("good") looks like you want to call it as if it were a function. That's not possible, so it says "str object is not callable".

You probably meant

if Feeling == "good":

You're also missing a : after the else , you shouldn't capitalize else, and the line after should be indented.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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