3 Replies - 1088 Views - Last Post: 23 November 2012 - 02:29 AM Rate Topic: -----

#1 divinum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-November 12

TypeError: 'int' object is not callable

Posted 22 November 2012 - 07:41 AM

 
version = "0.1.5"
#globals
global lobb
global cell
global skel
#objects
table  = 0
rug    = 0
window = 0
skel   = 0
#items
gold   = 0
key    = 0
dagger = 0
#room
lobb   = 0
cell   = 0
bed    = 0
yard   = 0

class room():
    def __init__(self, lobb, cell, bed, yard):
        self.lobb  = room
        self.cell  = room
        self.bed   = room
        self.yard  = room
room.lobb  = "Livingroom \n"
room.cell  = "Cellar \n"
room.bed   = "Bedroom \n"
room.yard  = "Yard \n"

class roomdesc():
    def __init__(self, lobb, cell, bed, yard):
        self.lobb  = room
        self.cell  = room
        self.bed   = room
        self.yard  = room
roomdesc.lobb  = "In this room you find: A trophy case, a suit of armor, and a large rug"
roomdesc.cell  = "There is very little light down here but you see a skeleton laying on the floor."
roomdesc.bed   = "There is a bed in one corner of the room."
roomdesc.yard  = "This is the yard outside of the house, there is nothing of interest here."


def wrong():
    print '\nYou can not go in that direction.'
    
def item():
    global dagger
    if dagger > 0:
        print '\nYou have a rusted, dull dagger.'
    else:
        print 'No items.'
        
        
def prompt():
    x = raw_input(">")
    return x

def start():
    print 'Grimoir Ver', version
    print '\nInteractive Fiction- A fantasy'
    print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n'
    lobby()

def lobby():
    global lobb
    global rug
    print room.lobb
    print roomdesc.lobb
    print 'Why dont we have a look at the trophy case? Type "look at case"'
    lobb =1
    while lobb == 1:
        cmd = prompt()
        if cmd == "n":
            print '\nComing soon.'
        if cmd == "d":
            if rug == 1:
                cell()
            if rug < 1:
                print '\nThere is a rug on the floor.'
        if cmd == "s" or cmd == "w" or cmd == "e" or cmd == "u":
            wrong()
        if cmd == "move rug":
            if rug == 0:
                print '\nYou pull feircly and the rug moves enough to reveal a trap door.'
                rug = 1
            else:
                print '\nThe rug has already been moved'
                lobb =1
        if cmd == "look at case":
            print '\nThe case has a small scrap of paper taped to the glass.'
            print 'The paper reads: \n"You can use the commands: n, s, e, w, d, u, take (item), look at (object), and remember do not capitalize words. \nIf you type in a command the game does not recodnize the ">" will just reappear."'
        if cmd == "look at armor":
            print '\nThe armor is rusted and of no use.'
        if cmd == "inventory":
            item()
        
def cell():
    global cell
    global dagger
    global skel
    global lobb
    print room.cell
    print roomdesc.cell
    cell = 1
    while cell == 1:
        cmd = prompt()
        if cmd == "u":
            lobby()
        if cmd == "s" or cmd == "w" or cmd == "e" or cmd == "d" or cmd == "n":
            wrong() 
        if cmd == "look at skeleton":
            if skel == 0:
                print '\nThere is a dagger sticking out of its chest..where its heart used to be.'
                print'\nYou have found a dagger.'
                dagger = 1
                skel   = 1
                cell   = 1
                
            elif skel == 1:
                print '\nIts a skeleton.'
                cell = 1
        if cmd == "inventory":
            item()
   
            
        
        

        
        

start() 




this is what i have so far and if i move the rug then go down then up then back down i get:

Traceback (most recent call last):
File "C:\Users\Jr\Documents\NetBeansProjects\test2\src\test2.py", line 133, in <module>
start()
File "C:\Users\Jr\Documents\NetBeansProjects\test2\src\test2.py", line 63, in start
lobby()
File "C:\Users\Jr\Documents\NetBeansProjects\test2\src\test2.py", line 78, in lobby
cell()
File "C:\Users\Jr\Documents\NetBeansProjects\test2\src\test2.py", line 109, in cell
lobby()
File "C:\Users\Jr\Documents\NetBeansProjects\test2\src\test2.py", line 78, in lobby
cell()
TypeError: 'int' object is not callable


any help would be appreciated and theres probably an easier way to do this isnt there?

Is This A Good Question/Topic? 0
  • +

Replies To: TypeError: 'int' object is not callable

#2 Tayacan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 275
  • Joined: 18-January 11

Re: TypeError: 'int' object is not callable

Posted 22 November 2012 - 08:57 AM

You've got an integer called cell (defined on line 17) and a function called cell (line 98). This causes a problem, and here's why:

In some languages, functions and variables have different namespaces - that is, the compiler always knows if you're referring to a function or a variable.

In Python, though, you can pass functions around as parameters to other functions. How is this useful? Let me give a simple example:
lst = [1,2,3,4]
for i in range(len(lst)):
    lst[i] = lst[i] + 1


The above code adds one to every number in lst. Now, what if you wanted to do something else to all the items in lst? You'd have to write the entire for-loop again. Instead, we can define the following function:
def map(f,lst):
    for i in range(len(lst)):
        lst[i] = f(lst[i])


Where f is, of course, a function. Then, we could simply do this:
lst = [1,2,3,4]
def plus1(n): return n+1
map(plus1,lst)


Now, this is of course incredibly smart, but the consequence is that you can't have a function and a variable of the same name. Imagine this:
foo = 3
def foo(n): return n*2
map(foo,[1,2,3])


Now, which foo do we pass to the map function? It may be pretty clear to a human what the intention is here - after all, map takes a function and not an integer - but Python does not enforce types, and hence has no way of knowing.

So the solution to your problem should be to give either the cell function or the cell variable a different name.

As to the rest of your code - it'll be a lot of work to turn it into something that's easier to work with. The first thing I'd suggest that you do is come up with a sensible class for representing a location. Judging by the room class you've made, you really need to learn how to think object oriented.

What do you need in order to represent a room? A description. A list of the items in there that the player can pick up. A list of exits.

Also... Please get rid of the globals. You don't need them. Globals are bad. You can do everything you want with member variables and function parameters.

This post has been edited by Tayacan: 22 November 2012 - 08:58 AM

Was This Post Helpful? 2
  • +
  • -

#3 divinum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-November 12

Re: TypeError: 'int' object is not callable

Posted 22 November 2012 - 09:18 PM

Quote

As to the rest of your code - it'll be a lot of work to turn it into something that's easier to work with. The first thing I'd suggest that you do is come up with a sensible class for representing a location. Judging by the room class you've made, you really need to learn how to think object oriented.

What do you need in order to represent a room? A description. A list of the items in there that the player can pick up. A list of exits.


this is probably really stupid but could you provide an example?

Quote

Also... Please get rid of the globals. You don't need them. Globals are bad. You can do everything you want with member variables and function parameters.


how would i do this?

also im new to python and im still learning how to do things.
Was This Post Helpful? 0
  • +
  • -

#4 Tayacan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 275
  • Joined: 18-January 11

Re: TypeError: 'int' object is not callable

Posted 23 November 2012 - 02:29 AM

Here's an example of a class that could describe a room:
class Room:
    def __init__(self,name,desc,objs,exits):
        self.name = name
        self.description = desc
        self.objects = objs
        self.exits = exits

    def lookAround(self):
        print("You are in the " + self.name)
        print(self.description)
        for obj in self.objects:
            print("There is a " + obj + " in the room.")


Then you can create your rooms like this:
lobbyDesc = "You see a cozy room with dark, old fashioned furniture" #I just made that up
lobbyObjs = ["large rug", "trophy case", "suit of armor"]
lobbyExits = ["north","west"] #I made those up too.

lobby = Room("lobby",lobbyDesc,lobbyObjs,lobbyExits)

lobby.lookAround()


That will print out the following text:
You are in the lobby
You see a cozy room with dark, old fashioned furniture
There is a large rug in the room.
There is a trophy case in the room.
There is a suit of armor in the room.


Of course, that is not a complete Room class. I'd probably add a method to pick up an item, removing it from the Rooms objects list and placing it in the players inventory (which could also be a list). Maybe you want a more specific description of an objects location, such as "There is a large rug on the floor" - how could you change the code to make this possible?

How does the player move around? I made a list for exits, but maybe a different format, one that allows more description as well as a destination, would be more useful. Like, lookAround() could also print something like this:

There is a large, wooden door to the west.
There is a stairway to the north.


And the program would need some way of knowing "if the player goes up the stairs, what room does he enter next?"

This post has been edited by Tayacan: 23 November 2012 - 02:35 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1