0

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
0TTT0
  • 1,288
  • 1
  • 13
  • 23
John
  • 37
  • 5
  • Maybe the problem is in the line skdbelsldjdwlslkdneñsldjdn – developer_hatch Oct 27 '17 at 23:49
  • Sorry just a little Friday joke :) – developer_hatch Oct 27 '17 at 23:49
  • 1
    Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. – Prune Oct 27 '17 at 23:51
  • 1
    Look at the error. It says `add_to_inventory` takes no arguments. Yet you tried to pass an argument when calling it. So make up your mind. Should it take an argument or not? If so, then *add it*. If not, then *don't call it with one*. It really doesn't get much more basic than this. – Tom Karzes Oct 27 '17 at 23:55
  • Voting to close - these bugs are at the level of typos (e.g., missing arguments). – Tom Karzes Oct 27 '17 at 23:56
  • how do you turn it into an argument? Sorry I am new to python... – John Oct 27 '17 at 23:57

1 Answers1

0

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)
0TTT0
  • 1,288
  • 1
  • 13
  • 23
  • 1
    wow, thank you so much! The second option seemed to solve my problem! – John Oct 28 '17 at 00:04
  • That `if` / `else` pattern is a common one, addressed by the standard libraries. Consider using `inventory = collections.defaultdict(int)`. https://docs.python.org/3/library/collections.html#collections.defaultdict – J_H Oct 28 '17 at 01:58