0

just wanted to get an opinion on my current although very thin skeleton of my text-based RPG I am creating within Python.

class Item(object):
    def __init__(self, item):
    if(key in Loot):
        self.item = Inventory[key]
    else:
        self.item = None
        print('No loot obtain for that decision')

    def get_item(self):
        return self.item

    def item(self):
        return str(self.item)

class Inventory(object):
    def __init__(self):
        self.inventory = []

    def add_item(self, item):
        self.inventory.append(item)
        return self.inventory

    def added_loot(self):
        print('Congrats, you have obtained a/an' + str(self.item))

My current code displays my inventory dictionary as values when I run the added_loot method. I wanted to see if there was any way I can remake this code and use keys in my dictionary as my self.inventory and shuffle those? So when I print out str(self.item) it will print out the actual key and not the value within the dictionary.

  • In that `Item.__init__`, what are `key` and `Loot` supposed to be? Also, `Inventory` is a class, not a dict, so what is `Inventory[key]` supposed to do? – abarnert Aug 07 '18 at 00:08
  • Meanwhile, storing something in `self.item` means you're hiding the method `item` defined in the class, so you'll never be able to call it. Also, you almost never want getter functions like `get_item` in Python. Also, why does `add_item` need to return anything? And how is `added_loot` supposed to work when an `Inventory` doesn't have an `item` attribute. – abarnert Aug 07 '18 at 00:10
  • Wow, sorry my question was structured very poorly. I intended key to called the keys in my Dictionary I created named Loot. Here is the example dictionary. – TechieslayJ Aug 07 '18 at 00:14
  • Loot = ('Sword':25, 'Dagger':10, 'Boomerang':2, 'Stick':1) – TechieslayJ Aug 07 '18 at 00:15
  • Also thank you so much for the information about hiding the method item and not being able to call that. That definitely explains a few other problems I am running in to early on in the game. – TechieslayJ Aug 07 '18 at 00:17

1 Answers1

0

You might want to consider rephrasing this, or checking out Code Review instead. Your question doesn't seem to ask for a solution to a specific problem, especially with your first line saying that you just want an opinion on your code. Stack Overflow isn't really the place for that.

As an answer to what seems to be the main question at the end, you have defined an Item() class, so you might consider adding a __str__ or __repr__ method. Then you can create an instance and print whatever string you've previously set up in said method with a simple print() call.

test = Item()
print(test)

Of course, that's going to require fixing your __init__ method which is improperly indented, and has no attributes.

Noved
  • 68
  • 1
  • 6