所以我希望能够删除和添加我在游戏中尝试创建的库存中的项目,但我一直收到错误。这是我的代码:
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()
这是我在运行游戏时收到的错误:
(拿起剃须刀) 你走过去拿起你的剃刀刀片。 它已添加到您的广告资源中。
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
答案 0 :(得分:0)
您的错误正在发生,因为add_to_inventory
在函数定义中没有参数,但您尝试将'razor'
作为参数传递给它。这就是TypeError: add_to_inventory() takes 0 positional arguments but 1 was given
的含义。
此外,您正在使用列表append
方法,但您正在将库存作为字典。
根据所需的功能,您可以使用以下两个选项:一个用于字典,另一个用于列表。
#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)