3

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.

2
  • 1
    It should give (1) an indentation error (I blame this on copy&paste though) and after that's fixed, it would (2) throw a NameError: name ... is not defined if you didn't choose a name that happens to collide with a built-in. Sadly, you did, so you get silent misbehaviour and a perfect example of one reason one shouldn't shadow built-ins. Try to print next before the if statement.
    – user395760
    Mar 26, 2011 at 16:15
  • Good point, @delnan! I was so busy explaining namespaces that I did not even notice the subtlety that next always exists because next() is a built-in function. :) Mar 26, 2011 at 16:17

3 Answers 3

2

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!

1

You need to assign the result of your call to action() in start() to something. E.g. next = action(). When action() is done executing, Python doesn't need the next variable that you created in it anymore, so it discards it. You can keep that result in another function by assigning the result of the function to a variable (in this case next in the function start()).

Happy Hacking!

1

I've edit your syntax. May be it is helping you

def action():
    nextt = raw_input (">> ")
    return nextt

def start():
    print"""
    You are in a dark room.
    There is a door to your right and left.
    Which one do you take?"""

def bear_room():
    print "You meet the bear..."

def cthulu_room():
    print "you meet the princess"

def dead(message):
    print message

def answ(nextt):
    if nextt == "left":
       bear_room()
    elif nextt == "right":
       cthulu_room()
    else:
       dead("You stumble around the room until you starve.")

start()
ok = action()
answ(ok)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.