Search code examples
pythoninventorytext-based

Can't figure out what is wrong with my inventory in my text-base game


So I want to be able to remove and add items in an inventory that I tried creating in my game but I keep receiving an error. This is my code:

inventory={}
def add_to_inventory():
    inventory.append()

elif choice == "use h on razor":
        print ("(pick up razor)")
        if "razor" in inventory:
            print ("You already got this item.")
            print ("")
            print ("Inventory: " + str(inventory))
        if "razor" not in inventory:
            print ("You walked over and picked up your razor blade.")
            print ("It's been added to your inventory.")
            add_to_inventory("razor")
            print("")
            print ("Inventory: " + str(inventory))
        game()

this is the error that I receive when I run my game:

(pick up razor) You walked over and picked up your razor blade. It's been added to your inventory.

Traceback (most recent call last):
  File "C:\Users\Owner\AppData\Local\Programs\Python\Python36-32\inferno_junction_2017-10-27.py", line 337, in <module>
    instructions_part_1()
  File "C:\Users\Owner\AppData\Local\Programs\Python\Python36-32\inferno_junction_2017-10-27.py", line 336, in instructions_part_1
    try_1()
  File "C:\Users\Owner\AppData\Local\Programs\Python\Python36-32\inferno_junction_2017-10-27.py", line 310, in try_1
    instructions_part_2()
  File "C:\Users\Owner\AppData\Local\Programs\Python\Python36-32\inferno_junction_2017-10-27.py", line 304, in instructions_part_2
    try_2()
  File "C:\Users\Owner\AppData\Local\Programs\Python\Python36-32\inferno_junction_2017-10-27.py", line 286, in try_2
    instructions_part_3()
  File "C:\Users\Owner\AppData\Local\Programs\Python\Python36-32\inferno_junction_2017-10-27.py", line 280, in instructions_part_3
    try_3()
  File "C:\Users\Owner\AppData\Local\Programs\Python\Python36-32\inferno_junction_2017-10-27.py", line 266, in try_3
    instructions_part_4()
  File "C:\Users\Owner\AppData\Local\Programs\Python\Python36-32\inferno_junction_2017-10-27.py", line 259, in instructions_part_4
    main()
  File "C:\Users\Owner\AppData\Local\Programs\Python\Python36-32\inferno_junction_2017-10-27.py", line 231, in main
    start()
  File "C:\Users\Owner\AppData\Local\Programs\Python\Python36-32\inferno_junction_2017-10-27.py", line 219, in start
    game()
  File "C:\Users\Owner\AppData\Local\Programs\Python\Python36-32\inferno_junction_2017-10-27.py", line 57, in game
    game()
  File "C:\Users\Owner\AppData\Local\Programs\Python\Python36-32\inferno_junction_2017-10-27.py", line 155, in game
    add_to_inventory("razor")
TypeError: add_to_inventory() takes 0 positional arguments but 1 was given

Solution

  • Your error is occurring because add_to_inventory has no parameters in the function definition, but you are trying to pass 'razor' as an argument to it. That's what TypeError: add_to_inventory() takes 0 positional arguments but 1 was givenmeans.

    Also, you are using the list append method, but you are making inventory a dictionary.

    Here are two options that you could use depending on the functionality desired, one for the dictionary and one for the list.

    #DICTIONARY INVENTORY
    
    from collections import defaultdict
    inventory = defaultdict(int)
    
    def add_to_inventory(item, amount):
        inventory[item] += amount
    
    #LIST INVENTORY 
    
    inventory = []
    def add_to_inventory(item):
        inventory.append(item)