字典值在for循环分配中未更改 [英] Dictionary value is not changed inside a for loop assignment

查看:98
本文介绍了字典值在for循环分配中未更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在使用学习python的困难方式来学习python.我正在尝试开发一个库存系统.目标是将其构建为将由会议室类拉出的类.我将使用字典的清单,以便在为每个房间创建清单时可以说出它们在房间中的位置.

So I am learning python using Learn python the hard way. I am trying to develop a inventory system. The goal is to build this into a class that will be pulled by the room class. I am going to use a dictionary for the inventory so that when I create the inventory for each room I can say where they are in the room.

所以在卧室里,我可以说床上有一把刀,而不仅仅是说你看到了刀,蜡烛和油.

So in the bedroom I can say that there is a knife on the bed rather than just saying you see knife, candle, oil.

现在,当有人说要拿刀时,我正在捡拾物品.我能够进行一个奇怪的搜索,并将该值设置为无",这样当有人在房间里看时它不会出现,但我似乎遇到了范围问题.我在该站点上阅读了多个其他问题,这些问题说,因为它是易变的对象,所以我不需要做Global dict,而当我尝试它时就遇到了错误.我能够编辑对象,但是当我退出if语句和for循环时,它将不会继续执行.

Right now I am working on picking up the item when someone says take knife. I am able to do a weird search and set that value to None so that when someone looks in the room it does not show up but I seem to be running into a scope issue. I read multiple other questions on this site that said since its a mutable object i don't need to do Global dict and when i tried that it got an error. I am able to edit the object but when i go back out of my if statement and for loop it won't carry over.

请帮助我:)

#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'}

重要提示:目前,仅当您拿着刀而不是弹头时,此方法才有效.我需要找出针对多个单词的项目的另一种解决方案.

Important note: This currently only works if you take the knife and not the Warhead. I need to figure out another solution for items with multiple words.

谢谢!

推荐答案

循环中的value与字典的实际值不同. 它只是对该值的引用,因此当您执行value = None时,实际上是在更改 保存新值None的引用的值,而不是字典的值.

The value inside your loop is different than the real value of the dictionary. It is just a reference to that value, so when you do value = None you actually change the value of the reference to hold the new value None and not the value of the dictionary.

为了更好地演示,这是在for key, value in inventory.iteritems():

To demonstrate it better this is before the assignment inside the for key, value in inventory.iteritems():

-------            -------------------------
|value|   -------> |value of the dictionary|
-------            -------------------------

这是在value = None

                   -------------------------
                   |value of the dictionary|
                   -------------------------
-------            ------
|value|   -------> |None|
-------            ------

您可以看到字典值没有改变.仅for循环的变量value 变化.此变量属于for循环的范围,此后将被丢弃.

As you can see the dictionary value does not change. Only the variable value of the for loop changes. This variable belongs to the scope of the for loop and after that it is discarded.

一种替代方法是代替value = None来做:

An alternative would be instead of value = None to do:

inventory[key] = None

这篇关于字典值在for循环分配中未更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆