Reputation: 165
I am very new to Python. running on windows while I wait for some stuff for my Pi. I am writing a game where the user travels through a tower via text inputs.
the user can use "a" "d" "w" to look around a room to collect sutff. but if the user has already looked in "a" (which is "look left") i need to let them know they have already been there. this is my code but it has an obvious flaw.
#if user hits 'a' they look left
def roomOneLeft():
print '-- You search some loose rubble and find some cloth'
return roomOneMoves()
#where the user selects a movement
def roomOneMoves():
left = 0
move = raw_input("")
if left == 1:
print 'you have already looked here'
return roomOneMoves()
if move == "a":
left = left + 1
roomOneLeft()
can i set "left" to static? and ant work out how to set it as global variable like java. this obviously doesn't work because when it returns, it sets itself back to 0. any help would be greatly appreciated!
Upvotes: 0
Views: 73
Reputation: 32310
To make a variable global, declare in the module scope, and then to change it in a function, use the global
keyword.
left = 0
def some_func()
global left
left = 1
That will allow you to edit global variables inside a function.
To address your static
variable question, I believe you cannot do this in python. See this question.
Upvotes: 1
Reputation: 35980
You can define "left" as global. Like this:
left = 0
#where the user selects a movement
def roomOneMoves():
global left
move = raw_input("")
if left == 1:
print 'you have already looked here'
return roomOneMoves()
if move == "a":
left = left + 1
roomOneLeft()
Upvotes: 2