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.
1 Answer 1
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.
returnbefore calling that function...anything after return is not executednextis a builtin function in python. You might consider renaming your variable to something else (next_is a viable alternative)