Как распечатать список объектов? - PullRequest
0 голосов
/ 16 июня 2020

Я пытался распечатать свои объекты в виде списка. Список - это инвентарь. Его следует распечатать, набрав i. Но у меня только ошибка, которая показывает в списке объекты, которые нельзя распечатать. Я все время получаю ошибку. Пожалуйста, не могли бы вы мне помочь?

def play():

    while True:
        Action_input = get_player_action()

        if Action_input in  ["n","North"]:
            print("Go North")
        elif Action_input in ["s","South"]:
            print("Go South")
        elif Action_input in ["e","East"]:
            print("Go East")
        elif Action_input in ["w","West"]:
            print("Go West")
        elif Action_input in ["i","I"]:
           print("Inventory")
           for i in inventory:
               print(inventory)
        else:
            print("Not an availble direction. Use only n,e,w,s,North,South,East, or West")

class Resources:
    def __init__(self):
        self.name = name
        self.description = description
        self.health = health

    def __str__(self):
        return " Name: " + self.name + "Description: " + str(self.description) + " Damage: " + str(self.damage)

class bread(Resources):
    def __init__(self):
        self.name = "bread"
        self.description = "A kind of food that is cheap and nice."
        self.health = 4


class pako(Resources):
    def __init__(self):
        self.name = "pako"
        self.description = "A long piece of wood that can be used as a weapon"
        self.damage = 10

class punch(Resources):
    def __init__(self):
        self.name = "punch"
        self.description = "Using the fist to strike"
        self.damage = 6

class owo(Resources):
    def __init__(self):
        self.name = "owo"
        self.description = "What you use to buy goods or services"
        self.value = 10


def get_player_action():
    return input("What is your move?")


def best_attack_move(inventory):
    Max_Attack = 0
    Best_attack_move = None
    for item in inventory:
        if item.damage > Max_Attack:
            Best_attack_move = item
            Max_Attack = item.damage

    return Best_attack_move


inventory = [bread(),pako(),punch(),owo()]

play()

Ошибка:

Каков ваш ход? I Inventory [<<strong> main .bread объект на 0x02ADC418>, <<strong> main .pako объект в 0x02ADC448>, <<strong> main .punch объект в 0x02ADC478>, <<strong> main объект .owo в 0x02ADC4A8>] [<<strong> main .bread объект в 0x02ADC418>, <<strong> main объект .pako в 0x02ADC448>, <<strong> main .punch объект в 0x02ADC478>, <<strong> main объект .owo в 0x02ADC4A8 >] [<<strong> main .bread объект в 0x02ADC418>, <<strong> main .pako объект в 0x02ADC448>, <<strong> main .punch объект в 0x02ADC478>, <<strong> main объект .owo в 0x02ADC4A8>] [<<strong> main .bread объект в 0x02ADC418>, <<strong> main объект .pako в 0x02ADC448>, <<strong> main .punch объект по адресу 0x02ADC478>, <<strong> main .owo объект по адресу 0x02ADC4A8>] Что вы делаете?

Ответы [ 2 ]

0 голосов
/ 16 июня 2020

простое решение вашей проблемы находится в l oop.

elif Action_input in ["i","I"]:
       print("Inventory")
       for i in inventory:
           print(i)

Заменить инвентарь на i. также есть несколько ошибок, как описано ниже.

В классе хлеба

class bread(Resources):
def __init__(self):
    self.name = "bread"
    self.description = "A kind of food that is cheap and nice."
    self.health = 4

заменить self.health на self.damage

и то же самое для класса owo

class owo(Resources):
def __init__(self):
    self.name = "owo"
    self.description = "What you use to buy goods or services"
    self.damage = 10

заменить self.value на

Ваш окончательный код должен быть похож на

def play():

while True:
    Action_input = get_player_action()

    if Action_input in  ["n","North"]:
        print("Go North")
    elif Action_input in ["s","South"]:
        print("Go South")
    elif Action_input in ["e","East"]:
        print("Go East")
    elif Action_input in ["w","West"]:
        print("Go West")
    elif Action_input in ["i","I"]:
       print("Inventory")
       for i in inventory:
           print(i)
    else:
        print("Not an availble direction. Use only n,e,w,s,North,South,East, or West")

class Resources:
    def __init__(self):
        self.name = name
        self.description = description
        self.health = health

    def __str__(self):
        return " Name: " + self.name + "\n Description: " + str(self.description) + "\n Damage: " + str(self.damage)

class bread(Resources):
    def __init__(self):
        self.name = "bread"
        self.description = "A kind of food that is cheap and nice."
        self.damage = 4


class pako(Resources):
    def __init__(self):
        self.name = "pako"
        self.description = "A long piece of wood that can be used as a weapon"
        self.damage = 10

class punch(Resources):
    def __init__(self):
        self.name = "punch"
        self.description = "Using the fist to strike"
        self.damage = 6

class owo(Resources):
    def __init__(self):
        self.name = "owo"
        self.description = "What you use to buy goods or services"
        self.damage = 10


def get_player_action():
    return input("What is your move?")


def best_attack_move(inventory):
    Max_Attack = 0
    Best_attack_move = None
    for item in inventory:
        if item.damage > Max_Attack:
            Best_attack_move = item
            Max_Attack = item.damage

    return Best_attack_move


inventory = [bread(),pako(),punch(),owo()]

play()
0 голосов
/ 16 июня 2020

Вам нужно поместить каждый экземпляр переменной inventory внутрь str(), а также.

 inventory = [str(bread()),str(pako()),str(punch()),str(owo())]

(Кстати, bread и owo не имеют атрибута повреждения, поэтому вы получите сообщение об этом)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...