user1815591
user1815591

Reputation: 21

Python 'str' object is not callable Python 2.7.3

I am currently writing a trade game, where users connect to a server, then trade with one another and earn money, etc. But when I try

if(input.lower() == 'sell'):
        sMaterial = raw_input('Material: ')
        if(sMaterial.lower() == 'gold'):
            sAmount = int(input('Enter amount: '))
            if(gold >= sAmount):
                mon = mon + (100 * sAmount)
            else:
                print 'You do not have enough', sMaterial

It throws the error

> sell
Material: gold
Traceback (most recent call last):
  File "Test.py", line 119, in <module>
    sAmount = int(input('Enter amount: '))
TypeError: 'str' object is not callable

I am using Linux, Python version 2.7.3, with Geany development environment. Thanks in advance.

Upvotes: 1

Views: 7579

Answers (3)

BrenBarn
BrenBarn

Reputation: 251568

You have overwritten the input function with a variable holding some data. Somewhere you did input = .... (You can see in the first line of your code that you're doing input.lower().) The solution is to change the part of your code that does this. Don't give your variables the same names as builtin functions or types.

Upvotes: 3

Andbdrew
Andbdrew

Reputation: 11895

you should do

sAmount = int(raw_input('Enter amount: '))

instead of

sAmount = int(input('Enter amount: '))

and you may want to do some exception handling somewhere in there too :)

Upvotes: 1

DSM
DSM

Reputation: 353479

This line:

if(input.lower() == 'sell'):

tells me that you must have bound the name input to a string at some point. So when you call

sAmount = int(input('Enter amount: '))

you're trying to pass the argument 'Enter amount: ' to the string input, hence: TypeError: 'str' object is not callable. Since it looks like you're using Python 2, you should probably be using raw_input anyhow, but this is another reason not to rebind the builtin names.

Upvotes: 10

Related Questions