I'm using Learn Python the Hard Way and exercise 35's extra credit says to simplify. I would like to create a function that will ask the user for variable next and then return it to the other functions.
in case I am making no sense...
def action():
next = raw_input (">> ")
return next
def start():
print"""
You are in a dark room.
There is a door to your right and left.
Which one do you take?"""
action()
if next == "left":
bear_room()
elif next == "right":
cthulu_room()
else:
dead("You stumble around the room until you starve.")
when I run it like this it always gives next as else.
You need to store the return value of the function somewhere; once it exits, the whole little indented namespace beneath the function goes away, along with the next variable. I think you really want:
next = action()
That way, with the function's little namespace destroyed, you will still have a copy of next out at the top level of your program.
If this feature of Python sounds needlessly destructive, trust me: it is far easier to manage complex programs if you can count on each function being its own little world, that does not make global changes to the variables you have defined!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With