Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How do i make a text shop - Python 3

+6 votes
asked Jan 7, 2021 by ARTY (190 points)
So I need to make a text shop in python for a game and I want to add a sword in it that let's you defeat enemies that you could not defeat before. I also need to add the sword to the inventory to show the player he has it

1 Answer

+3 votes
answered Jan 8, 2021 by Peter Minarik (88,520 points)
edited Jan 8, 2021 by Peter Minarik

Think about what you need for your game.

  • What classes do you need for this?
  • What functionality would these classes offer?
  • What relations would be there between the classes?

I could think of the following.


  • ListItems()
  • Sell(ItemId)

Sword : Item

  • Use()


  • Equip(Item)

Give it a go, think about how you'd do it. Implement it and come back and show us how it went.


I was a bit bored, so I did quick implementation. Feel free to make it better:

class Hero:
    def __init__(self):
        self.items = []

    def Add(self, item):
    def Has(self, itemName):
        for item in self.items:
            if (item.name == itemName):
                return True
        return False
    def Die(self):
        print('The hero has died a terrible death')

class Monster:
    def Fight(self, hero):
        if (hero.Has('Sword')):
    def Die(self):
        print('The monster has been slain.')

class Sword:
    def __init__(self):
        self.name = 'Sword'

class HealingPotion:
    def __init__(self):
        self.name = "Healing potion"

class Shop:
    def __init__(self):
        self.items = [Sword(), HealingPotion()]
    def ShowItems(self):
        print('Available items in store:')
        index = 0
        for item in self.items:
            print(str(index) + '.\t' + item.name)
            index = index + 1
    def Purchase(self, itemIndex): # itemIndex is 0-based
        if (itemIndex < 0):
            print('Index must be >= 0')
            return None
        length = len(self.items)
        if (itemIndex >= length):
            print('Index must be < ' + str(length))
            return None
        return self.items[itemIndex]

hero = Hero()
shop = Shop()
itemId = int(input('Which item would you like to purchase (enter ID only): '))
item = shop.Purchase(itemId) # 0 --> Sword; 1 --> Healing potion
if (item != None):
    print('Purchased a ' + item.name + '.')
    print('Nothing purchased.')
monster = Monster()
commented Jan 8, 2021 by ARTY (190 points)
Thanks it's perfect
commented Jan 28, 2021 by Jeff The Chicken (2,920 points)
what if you wanted the hero to buy one, none, or both of the items?
commented Feb 5, 2021 by Jeff The Chicken (2,920 points)
you could have  a for loop that returns when the user decides
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.