Thursday, August 4, 2011

Python woes



Woof, I don't understand variable scope in Python.

I have what I thought was a global variable called "wumpus_room" that is a random whole number between (and including) 0 to 3. The script is borking at the fact that I have a line that could alter this variable inside of a function. If I comment out the block starting with

elif choice.lower() == "shoot north":

then there is no complaint.

This would not be confusing if it was crabbing about trying to alter a variable out of scope, but it gives a cryptic error:

UnboundLocalError: local variable 'wumpus_room' referenced before assignment

See.... I'd BELIEVE that if not for the fact I can comment out that reassignment line and everything works fine - this tells me the variable is indeed getting assigned!

How the heck do I declare a global variable and then edit that variable if necessary?!

No comments:

Post a Comment