Reputation: 3
I started a high school computer game programming class in Python last month, and the current project is to write a very basic text adventure game. While I've ran into some issues, I've managed to find solutions either by reading Python tutorials or reading past questions on Stackoverflow. However, I've been stuck on a single issue for the last two days which I've been unable to solve. I've been trying to change a variable to an input, which usually works seamlessly. However, in this case it appears to have a rather short memory that the variable was changed. Also sorry in advance for the messy code and any mistakes posting it on the forums; I am new to both.
#actions for room one
def roomOne():
if adultDragon == "1": #later part of puzzle
print ("To be continued...") #change this later
elif clothes == "0" and bread == "0":
print ("You look around the room that you're in. It's very bare, with a tattered wood cabinet on the south wall right next to an opening that leads to the next room.")
time.sleep(3)
action = input ("You should probably find a way to get out of this cave. ") #should be saving variable??
while action != "go south" and action != "south" and action != "cabinet" and action != "open cabinet" and action != "door" and action != "open door":
action = input ("I don't understand. ")
#error here: as soon as input is equal to one of the options, it turns "action" into "look around" WHY? (Look around was a user input earlier in the script... but it should've been changed using the last user input)
#use a variable as a workaround? #it's remembering action from previous script... possible local/global issue? Trying different variable may work? (Edit: tried changing variable name eg. "actionOne" and result was the same. Last user input is not being stored.)
if action == "go south" and action == "south":
print ("You approach the wooden cabinet.")
elif action == "cabinet" and action == "open cabinet":
print ("You can't reach that from here. ")
elif action == "door" and action == "open door":
print ("You don't feel strong enough to leave the room yet. ")
roomOnePuzzle() #prompts for the room's puzzle
def roomOnePuzzle(): #Thinks action is what it was previously (see above)
print (action) #only for testing purposes to find out what it thinks "action" is at this point... appears to think it is the previous user input instead of current input.
Should I post the entire code? The above is the 1/3 that I felt was relevant to the error... as you can see I haven't gotten that far into the game yet. I hate asking for help but I'm starting to get worried at being at an impasse for so long with the assignment due date quickly approaching.
Upvotes: 0
Views: 1594
Reputation: 121973
How could:
if action == "go south" and action == "south":
ever evaluate True
? Instead, you need:
if action == "go south" or action == "south":
# ^ note
Or, more Pythonic:
if action in ('go south', 'south'):
You can use this in your loop, too:
while action not in ('go south', 'south', ...):
Better still, use a dictionary:
actions = {'go south': "You approach the wooden cabinet.",
'south': "You approach the wooden cabinet.",
...}
action = input(...).lower()
while action not in actions:
action = input(...).lower()
print(actions[action]) # no need for if
room_one_puzzle(action) # note PEP-8 name and explicit argument
Finally, don't rely on scoping for access to the values you need, refactor for explicit arguments and return values; for example, clothes
and bread
could be in an inventory
dictionary that is passed to each function.
Upvotes: 1