无法弄清楚我的 text-base 游戏中的库存有什么问题
Can't figure out what is wrong with my inventory in my text-base game
因此,我希望能够在我尝试在游戏中创建的库存中移除和添加物品,但我一直收到错误消息。这是我的代码:
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()
这是我在 运行 我的游戏中收到的错误:
(拿起剃须刀)
你走过去拿起你的剃须刀 blade。
它已添加到您的库存中。
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
您的错误发生是因为 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)
因此,我希望能够在我尝试在游戏中创建的库存中移除和添加物品,但我一直收到错误消息。这是我的代码:
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()
这是我在 运行 我的游戏中收到的错误:
(拿起剃须刀) 你走过去拿起你的剃须刀 blade。 它已添加到您的库存中。
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
您的错误发生是因为 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)