Sign in to follow this  
PaladinJohn

Returning a value in a function with Python

Recommended Posts

PaladinJohn    130
I just recently started teaching myself Python a few days ago and I would like to know a little bit more in regards to returning a value from a function. Specifically, I made a very basic program that is supposed to act as a text based moving from room to room sort of thing. My goal is to be able to select a direction within the function and send that choice outside of the function to influence the next function that is activated. Here is my source code for the program so far:

[code]def start():
print """
Lots of text about the first room.
"""

keepGoing = True

while keepGoing:
direction = raw_input("Which direction will you take? (North or South): ")
direction = direction.lower()
if direction == "north":
keepGoing = False
elif direction == "south":
keepGoing = False
else:
print
print "I'm sorry, but that is not a valid direction."
print

return direction

def church():
print """
Lots of text about the second room.
"""

def road():
print """
Lots of text about the third room.
"""

start()

if direction == "north":
church()
elif direction == "south":
road()
else:
print "Something went wrong."[/code]

Through running this program in the debugger I am positive that the problem is that 'direction' is not being returned from the function, leading me to believe I don't know how to properly return a value from a function. If any of you who are more experienced with this sort of thing can show me what I did wrong, with as few changes to this program as possible, I would appreciate it. I've made this program run via different methods, but I would like to understand how to return values from a function properly since that seems like an imperative skill going forward.

Thanks in advance for taking a look, I really appreciate it!

Share this post


Link to post
Share on other sites
bubu LV    1436
You must assign variable returned from function call:
[code]direction = start()[/code]

This is no different from C/C++, VisualBasic, Pascal, etc...

Share this post


Link to post
Share on other sites
PaladinJohn    130
Ugh... I knew it was going to be something that simple.

For what ever reason when I was doing the program last night, I figured just running the function would be fine and never thought that I would have to actually declare 'direction' as it would automatically be imported from the function. Now I see that it only returns the value and that value needs to be assigned. Also, that any time start() is called, whether by itself or listed in an assignment, it will be run, which explains why sometimes when tinkering with this I would run into an infinite start() loop.

Thank you very much! Now that I understand how functions work I can continue on with some more complex stuff.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this