Python: Locally assigning values to a global list -


i in process of programming text-based adventure in python learning exercise. want "help" global command, stored values in list, can called @ (essentially) time. player enters new room, or options change, reset help_commands list new values. however, reason cannot values in help_commands update inside function.

i asked similar question before (python: typeerror: 'list' object not callable on global variable) , suggested object might way me go.

i'm new python , objects 1 of weaker aspects, possibly example someone?

player = {     "name": "",     "gender": "",     "race": "",     "class": "",     "hp": 10, }  # global help_commands help_commands = ["save", "quit", "other"]  def help():     sub_help = ' | '.join(help_commands)     return "the following commands avalible: " + sub_help   def help_test():     print help()     help_commands = ["exit [direction], open [object], talk [person], use [item]"]     print help()     print "before go further, i'd know little more you."     print "what name, young adventurer?"     player_name = raw_input(">> ").lower()     if player_name == "help":         help()     else:         player['name'] = player_name         print "it nice meet you, ", player['name'] + "."  help_test() 

it's creating local variable same name global variable. in all, it's not great way program - might want have global object, instead... or not. in event, in help_test() function, start with:

global(help_commands) 

and see if want.

this happens in objects, too.... there's difference between:

foo = "something" 

and

self.foo = "something" 

inside member function. if you're coming visual basic, concepts these royally screw (luckily went other way, , shake head @ vb).


Comments