python - Dictionary value is not changed inside a for loop assignment -



python - Dictionary value is not changed inside a for loop assignment -

so learning python using larn python hard way. trying develop inventory system. goal build class pulled room class. going utilize dictionary inventory when create inventory each room can in room.

so in sleeping room can there knife on bed rather saying see knife, candle, oil.

right working on picking item when says take knife. able weird search , set value none when looks in room not show seem running scope issue. read multiple other questions on site said since mutable object don't need global dict , when tried got error. able edit object when go out of if statement , loop won't carry over.

please help me :)

#this inventory in room inventory = {'desk': 'miniature fusion warhead', 'bed':'knife', 'sink':none} def take(item): if item in inventory.itervalues(): #add_item_to_inventory(item) key, value in inventory.iteritems(): if value == item: print ("wow.\n" "look @ you.\n" "picking shit , everything.") value = none else: print ("what think is?" " dollar store? don't " "got shit here!") # prints out items in room. # hoping later come thought how # create different variations of this, , have randomly # pick between different ways says sees. key, value in inventory.iteritems(): if value != none: print "you see %s on %s." % (value, key) print "what want 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 on grab %s." "\n...") % split_ui[1] take(split_ui[1]) else: print ("what talking bout willis? " "don't know " "takin shit.") else: print ("who taught how talk?" "\n...\nlet me show how done.\n" "use verb, take, throw in " "object knife.") print inventory

this output given.

you see knife on bed. see miniature fusion warhead on desk. want pick up? >take knife ['take', 'knife'] reach on grab knife. ... wow. @ you. picking shit , everything. {'sink': none, 'bed': 'knife', 'desk': 'miniature fusion warhead'}

important note: works if take knife , not warhead. need figure out solution items multiple words.

thanks!

the value within loop different real value of dictionary. reference value, when value = none alter value of reference hold new value none , not value of dictionary.

to demonstrate improve before assignment within for key, value in inventory.iteritems():

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

this after value = none

------------------------- |value of dictionary| ------------------------- ------- ------ |value| -------> |none| ------- ------

as can see dictionary value not change. variable value of for loop changes. variable belongs scope of for loop , after discarded.

an alternative instead of value = none do:

inventory[key] = none

python dictionary

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -