0

I have a function as part of a game for a tutorial. The function in question should trigger another function if a condition is met (if theobject == "code")

# right room
def right_room():
 print "You see a table with two objects: a map and a code translator"
 print "You can take one object"
 print "Which object do you take?"
 next = raw_input("> ")
 if "map" in next and "code" in next:
 dead("You're greed surpassed your wisdom.")
 elif "map" in next:
 print "OK, you have the map."
 theobject = "map"
 print "Now you must exit and go ahead"
 return theobject
 opening()
 elif "code" in next:
 print "OK, you have the code."
 theobject = "code"
 print "Now you must exit and go ahead."
 return theobject
 opening()

But opening is not called? Here is the output:

You're in a Labrynthe. There's a door on your left. There's a door on your right. Or you can go ahead.

right You see a table with two objects: a map and a code translator You can take one object Which object do you take? code OK, you have the code. Now you must exit and go ahead.

The function above is then meant to send the person back to the start and hints at them to input "ahead" in the terminal:

# opening scene
def opening():
 print "You're in a Labrynthe."
 print "There's a door on your left."
 print "There's a door on your right."
 print "Or you can go ahead."
 next = raw_input("> ")
 if "right" in next:
 right_room()
 elif "left" in next:
 left_room()
 elif "ahead" in next:
 ahead()
 else: 
 print "Which way will you go?"

But opening() is not called. Instead Python seems to complete the script and exit.

asked Dec 18, 2013 at 0:09
2
  • 4
    You are using return before calling that function...anything after return is not executed Commented Dec 18, 2013 at 0:11
  • 1
    next is a builtin function in python. You might consider renaming your variable to something else (next_ is a viable alternative) Commented Dec 18, 2013 at 1:39

1 Answer 1

2

The return statement in Python (and almost all languages -- the notable exception of Haskell --) means that the function should stop there. If the statement is return expr then the value of the expression is available to the caller. Otherwise, in Python, the value None is available to the caller.

So you might need to move the function call just before the return statement.

answered Dec 18, 2013 at 0:12
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that has sorted it. Accepting when the timer comes off

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.