所以我正在学习使用Learn python的python。我正在努力开发一个库存系统。目标是将其构建为一个将由房间类拉出的类。我将使用字典作为库存,这样当我为每个房间创建库存时,我可以说出它们在房间里的位置。
所以在卧室里,我可以说床上有一把刀,而不只是说你看到刀,蜡烛,油。
现在,当有人说拿刀时,我正在努力拿起物品。我能够做一个奇怪的搜索,并将该值设置为无,以便当有人在房间里看起来它没有显示但我似乎遇到了范围问题。我在这个网站上阅读了多个其他问题,因为它是一个可变对象,我不需要做全局字典,当我尝试它有错误。我能够编辑对象但是当我退出我的if语句并且for循环时它不会继续。
请帮帮我:)。
#This is the inventory in this room
inventory = {'desk': 'Miniature Fusion Warhead',
'bed':'knife', 'sink':None}
def take(item):
if item in inventory.itervalues():
#add_item_to_inventory(item)
for key, value in inventory.iteritems():
if value == item:
print ("Wow.\n"
"Look at you.\n"
"Picking shit up and everything.")
value = None
else:
print ("What do you think this is?"
" The dollar store? We don't "
"got that shit here!")
# Prints out what items are in the room.
# I am hoping to later come up with an idea for how
# To make different variations of this, and have it randomly
# pick between the different ways it says what it sees.
for key, value in inventory.iteritems():
if value != None:
print "You see a %s on the %s." % (value, key)
print "What do you want to pick up?"
ui = raw_input(">")
split_ui = ui.split()
print split_ui
if len(split_ui) > 1:
if split_ui[0] == "take":
print ("You reach over to grab the %s."
"\n...") % split_ui[1]
take(split_ui[1])
else:
print ("What you talking bout Willis? "
"Don't you know this is just about "
"takin shit.")
else:
print ("Who taught you how to talk?"
"\n...\nLet me show you how its done.\n"
"Use a verb, like take, then throw in an "
"object like knife.")
print inventory
这是我给出的输出。
You see a knife on the bed.
You see a Miniature Fusion Warhead on the desk.
What do you want to pick up?
>take knife
['take', 'knife']
You reach over to grab the knife.
...
Wow.
Look at you.
Picking shit up and everything.
{'sink': None, 'bed': 'knife', 'desk': 'Miniature Fusion Warhead'}
重要提示:目前只有你拿刀而不是弹头才有效。我需要找出具有多个单词的项目的另一种解决方案。
谢谢!
答案 0 :(得分:4)
循环中的value
与字典的实际值不同。
它只是对该值的引用,因此当您执行value = None
实际更改时
保存新值None
的引用值,而不是字典值。
为了更好地展示它,这是在for key, value in inventory.iteritems():
------- -------------------------
|value| -------> |value of the dictionary|
------- -------------------------
这是在value = None
-------------------------
|value of the dictionary|
-------------------------
------- ------
|value| -------> |None|
------- ------
正如您所看到的,字典值不会改变。只有value
循环的变量for
变化。此变量属于for
循环的范围,之后将被丢弃。
替代value = None
代替:
inventory[key] = None
答案 1 :(得分:1)
zbs是正确的,你只是将指针的值改为dict值。但是,你这样做太难了:
#This is the inventory in this room
inventory = {'Miniature Fusion Warhead': 'desk',
'knife':'bed'}
player_inventory = set()
def take(item):
if item in inventory:
print("You picked up the {}".format(item))
player_inventory.add(item)
del inventory[item]
else:
print("That item doesn't exist")
while True:
print('')
print("Inventory: " + ', '.join(player_inventory))
for k,v in inventory.items():
print("You see a {} on the {}".format(k, v))
print("What do you want to pick up?")
ui = raw_input("> ").split()
verb = ui[0]
item = ' '.join(ui[1:])
if verb == 'take':
if item:
print("You take the {}".format(item))
take(item)
else:
print("That item doesn't exist")
else:
print("That's not an action")