0

私はテキストアドベンチャーに取り組んでいます。部屋のクラスとアイテムのクラスがありますが、部屋の中に複数のアイテムを置くと問題が発生します。コードの例を次に示します。

class Place(object):
"""The Room class"""
    def __init__(self, name, Item):
        self.name = name
        self.Item = Item


class Item():
"""The Item class"""
    def __init__(self, name):
        self.name = name


PC = Item("PC")
Bed = Item("Bed")   

bedroom = Place(name = "Bedroom", Item = {PC, Bed})

print (bedroom.Item.name)

そしてエラーメッセージ:

Traceback (most recent call last):
File "C:\Documents and Settings\Kenneth\My Documents\Python_Projects\projects\
TA.Project\location\classes2.py", line 18, in <module>
print (bedroom.Item.name)
AttributeError: 'set' object has no attribute 'name'
Press any key to continue . . .

これはアイテムが 1 つしかない場合に機能しますが、さらに追加しようとするとエラーが発生します。一度に 1 つの項目を表示する for ループを作成しようとしましたが、それも機能しませんでした。どんな助けでも大歓迎です。ありがとうございました。

私が試したループは、次のようなものでした。

for i in bedroom.Item:
    print (i)

Attributeerror 'Place' オブジェクトに属性 'Item' がありません

回答は受け入れられます。同様の問題を抱えている他の人のためにコードを更新したいと思います。

class Place:
    def __init__(self, name: str, items: set):
        self.name = name
        if items is None:
            items = set()   
        self.items = items

    def get_item_names(self):
    # one-liner: return [x.name for x in self.items]
        names = []
        for item in self.items:
            names.append(item.name)
        return '\n'.join(names)


    def get_item_value(self):
        value = []
        for item in self.items:
            value.append(item.value)
            return value


class Item:
    def __init__(self, name: str, value: iter):
        self.name = name
        self.value = value


Bed=Item("Bed", 200)
Chair=Item("Chair", 10)
Oven=Item("Oven", 500)


bedroom = Place("Bedroom", {Bed, Chair})
kitchen = Place("Kitchen", {Oven})


print (bedroom.get_item_names)

値関数は、Item クラスがまだ機能していることを証明するためのものでした。

4

1 に答える 1

1

クラス名を変数名として再利用しないでください! これは混乱を招き、エラーにつながるだけです。:)

私にとってはbedroom.Item問題なく動作しますが、大文字と小文字を入れ替えたのではないでしょうか?!

AttributeError: 'Place' object has no attribute 'Item'Place-class が variable を提供しないことを意味しますItem。したがって、実際に変数にitemorという名前を付けた場合Items、 ではなく、それを要求する必要がありますItem


質問の更新前にすでに書かれています:

1 つの部屋で複数のアイテムを使用すると、特定のアイテムが 1 つだけではなく、アイテムPlaceのセットになります。したがって、bedroom.Item.name実際に実行するときはset、に格納されbedroom.Itemsている を要求していますname(これは提供されないため、エラーになります)。

明確な定義をお勧めします。常に複数のアイテムを期待しましょうPlace(おそらく1つだけですが、それがまだセット内にある場合、私の提案はとにかく機能します).

例:

class Place:
    def __init__(self, name: str, items: set):
        # The `items: set` notation is only for better understanding.
        # It is valid Python3 syntax, but does not ensure the actual type!
        self.name = name
        if items is None:
            item = set()   # this is to avoid possible erros later.
        self.items = item

    def get_item_names(self):
        # one-liner: return [x.name for x in self.items]
        names = []
        for item in self.items:
            names.append(item.name)
        return names

class Item:
    def __init__(self, name: str):
        self.name = name

bedroom = Place("Bedroom", {Item("Bed"), Item("Chair")})
kitchen = Place("Kitchen", {Item("Oven")})

# usage:
>>> print(bedroom.get_item_names())
["Bed", "Chair"]
>>> print(kitchen.get_item_names())
["Oven"]

get_item_names主な違いは、これらすべての名前を取得するためのループ (の ) の使用です。アイテム自体を追跡したい場合でも、items反復できる -set を取得できます。

>>> for item in bedroom.items:
...     print(item.name + ":", item)
Bed: <__main__.Item object at 0x7f0c081327f0>
Chair: <__main__.Item obect at 0x7f0c08132898>
于 2015-03-06T09:36:46.790 に答える