Search code examples
pythonpython-2.7inventory

Text based adventure game inventory problems python


I have been working on a horror texted based adenture game & I am having some inventory problems. The inventory is an array that I can call in any function. I kind of got there but am just repopulating my array with a new inventory every time. I could use some help, these are my inventory functions:

#Populating an aray with items to be used throughout the game.
def createItems():
   items = range(0, 11)
   if items[9] != "full":
      items[1] = ("Axe")
      items[2] = ("Gas")
      items[3] = ("keys")
      items[4] = ("gun")
      items[5] = ("note")
      items[9] = ("full")
      return items
   else:
      return items 
# this function is going to check if the item passed to it is still in the array
def checkItems(item):
  list = createItems()
  itemC = item
  for i in range (0, 11):
    if list[i] == itemC:
      return ("no")
      break

def createInventory():
   inv = range(0 , 11)
   inv[10] = ("made")
   if inv[10] != ("made"):
      for i in range (0, 11):
         inv[i] = 0
   return inv

def stockInventory(item):
  inv = createInventory()
  for i in range (0, 11):
     if inv[i] == 0:
       inv[i] = item 
       break
       return inv

def checkInventory(item):
    itemC = item
    inv = createInventory()
    for i in range(0, 11):
       if itemC == inv[i]:
          return ("yes")

Solution

  • This may not be the answer, but from what I could make out from the code & the question, this should help. Note the differences in your code & mine & make changes accordingly.

    # Main Inventory
    Inventory = createInventory()
    
    # Populating given inventory aray with items to be used throughout the game.
    def createItems(inv):
        items = inv
        items[1] = "Axe"
        items[2] = "Gas"
        items[3] = "keys"
        items[4] = "gun"
        items[5] = "note"
        items[9] = "full"
    
    # Check if the item passed to it is still in the inventory array
    def checkItems(item):
        items = Inventory
        for i in range(len(items)):
            if items[i] == item:
                return "yes"
        return "no"
    
    def createInventory():
        inv = range(11)
        inv[10] = "made"
        return inv
    
    def stockInventory(item):
        inv = Inventory
        for i in range (11):
            if inv[i] == 0:
                inv[i] = item
                break
        return inv
    
    def checkInventory(item):
        inv = Inventory
        for i in range(0, 11):
            if item == inv[i]:
                return "yes"
        return "no"