Python类Shenanigans

时间:2014-09-25 11:29:02

标签: python class object attributes

我不知道如何解决这个问题。我在Python中编写脚本非常新,所以我可能只是忽略了一些东西或只是一个全面的菜鸟。 有人可以解释我的代码有什么问题吗?

我的想法是创建两种类型的对象,房间和项目。这很有效,我在最后创建了一个房间和两个项目。然后,我们的想法是将一个项目放入一个房间的库存中,该库存实际上是一个对象列表。但是,我似乎无法将对象添加到房间。我已经尝试过解决我的课程的不同属性,但我没有尝试过任何工作!

请帮帮忙? :)

import sys
import os

## GLOBAL DEFINITIONS
global rooms, room
global objects, obj, allobjects
global commandlist, command
global here

## VARIABLE SET

here = 0
rooms = []
objects = []

## CLASSES START
class Object:
    """Object Set"""
    def __init__(self, name, description):
        self.name = name
        self.description = description
        return

    def show(self):
        print self.description
        return

class Inventory:
    """Inventory for Objects"""
    def __init__(self, target):
        self.objects = []

    def create(self, objects, target):
        self.objects = list(objects)
        self.target = str(target)
        return

    def addobject(self, obj):
        if len(self.objects) < 1:
            self.objects = []
        inv = self.inventory.objects
        inv.append(objects[int(obj)])
        self.inventory = inv
        print str(self.objects[(len(self.objects)-1)])[0], "added to", target
        return

class Room:
    """Single Room Data"""
    def __init__(self, name, description):
        self.name = name
        self.description = description
        self.inventory = Inventory(self)
        #xits = (n,s,w,e,u,d)
        exits = (0,0,0,0,0,0)
        return

    def show(self):
        room = (self.name, self.description)
        print self.name.upper()
        print self.description
        if len(self.inventory.objects) > 0:
            print "Objects:"
            for obj in self.inventory.objects:
                print obj
        return

## FUNCTIONS START

def create(obj, name, description):
    """Create Object"""
    if obj == "room":
        rooms.append(name)
        thisroom = rooms[int(len(rooms)-1)] = Room(name, description)
        thisroom.inventory = Inventory(thisroom)

        current_array = rooms
    elif obj == "item":
        objects.append((name, description))
        current_array = objects

    if name == "": name = "EMPTY"
    if description == "": description = "No Description"
    ret = "Object created - Type," + str(obj).upper() + "\n\
    Name: " + name + " (#" + str((len(current_array))-1) + ")\n\
    Description:\n  " + description
    print ret
    return

def give(obj, target):
    """Add Object to Target"""
    obj = int(obj)

    item = Object(str(objects[obj])[0], str(objects[obj])[1])
    rooms[int(target)].inventory.addobject(item)
    return

def look(target):
    if target==False: target = here
    rooms[int(target)].show()
    return

create("room", "Ground Zero", "First Empty Room")
create("item", "Item Zero", "A non-descript item.")
create("item", "Item One", "A non-descript item.")
give(0, 0)
look(here)

返回:

Traceback (most recent call last):
  File "test.py", line 105, in <module>
    give(0, 0)
  File "test.py", line 94, in give
    rooms[int(target)].inventory.addobject(item)
  File "test.py", line 42, in addobject
    inv.objects.append(objects[int(obj)])
AttributeError: Object instance has no attribute '__trunc__'

1 个答案:

答案 0 :(得分:0)

首先,DON&#39; T使用全局变量。而只是在你的主要声明变量。

if __name__ == "__main__":
    myroom = Room("Ground Zero", "First empty room")
    ...

以下是文件的外观。

## CLASSES START
class Object(object):
    """Object Set"""
    def __init__(self, name, description):
        self.name = name
        self.description = description
    # end Constructor

    def show(self):
        print self.description
    # end show

    def __str__(self):
        return self.description
    # end str magic method
# end class Object


class Inventory(object):
    """Inventory for Objects"""
    def __init__(self, target):
        self.objects = []
        self.target = target
    # end Constructor

    def create(self, objects, target):
        self.objects = list(objects)
        self.target = str(target)
    # end creates

    def show_objects(self):
        for i in self.objects:
            print i
    # end show_object

    def addobject(self, obj):
        self.objects.append(obj)
        print str(obj), "added to", str(self.target)
    # end addobject
# end class Inventory

class Room(object):
    """Single Room Data"""
    def __init__(self, name, description):
        self.name = name
        self.description = description
        self.inventory = Inventory(self)

    def show(self):
        print self.name.upper() # Probably want .title() instead
        print self.description
        self.inventory.show_objects()

    def create(self, name, description):
        self.append(name)
    # end create

    def __str__(self):
        """Str method display the name."""
        return str(self.name)
    # end __str__
# end class Room

if __name__ == "__main__":
    ground_zero = Room("Ground Zero", "First Empty Room")
    item_zero = Object("Item Zero", "A non-descript item.")
    item_one = Object("Item One", "A non-descript item.")
    ground_zero.inventory.addobject(item_zero)
    ground_zero.inventory.addobject(item_one)
    ground_zero.show()