"), age = raw_input("Enter your age > ") print "OK %r looks, your only %s so you c"/> "), age = raw_input("Enter your age > ") print "OK %r looks, your only %s so you c"/> "), age = raw_input("Enter your age > ") print "OK %r looks, your only %s so you c"/> "), age = raw_input("Enter your age > ") print "OK %r looks, your only %s so you c"/>
Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 21 Online
OpenStudy (dmw_ocw):

I have this function: def get_info(): name = raw_input("Enter your name > "), age = raw_input("Enter your age > ") print "OK %r looks, your only %s so you can still play games.\n" % (name, age) return (name, age) Can I use the return of name and age as a variable in another function?

OpenStudy (anonymous):

@dmw_ocw If the syntax is correct for the differnet type, Yes. \(\color{red}{Chance}\)

OpenStudy (dmw_ocw):

When trying to do this I am getting global name not define. My next function is below: def start(): opening = """\n%s are walking a path in the woods and come to a fork with a sign: Left or Right.\n Which way do you go? Enter Left or Right?""" % name

OpenStudy (anonymous):

You should be able to find the error, it is pretty simple error in the syntax. \(\color{blue}{Chance}\)

OpenStudy (e.mccormick):

Generally, no. Most languages do not allow the return statement to pass back multiple variables. In Python the solution is to pass back a tuple with the multiple bits of data in it, then break them out if needed.

OpenStudy (e.mccormick):

Oh, and because it is a tuple, you need to be assigning it as such in the call. As for why it would say it is not defined, that is because you have not defined it! Your function is returning an object. Are you assigning that object to anything? The scope of any function is local to itself. When the function ends, all variables used inside the function are destroyed by the garbage collector. Because name is being made inside the function, it does not exist outside the function. If you assign it to another local or global variable called name, it can survive this process.

OpenStudy (e.mccormick):

In other languages you end up passing the address to an array and modifying the array... sort of a cheat because you are not really passing the value about! Instead you pass a pointer, but that is how coding works. The down side is that it can cause unintended consequences! When you modify what is being pointed at, the original is changed. If you needed those values in their original form as well, oh well.... Python insulates from a lot of this. It deals in objects all the time. So what variables are global or local, and so on can be a little odd. The more local something is, the more that is what will be used! ``` # Some globals. x=2 y=3 # My assignent funtion that will use locals the same name as the globals! def func(): x=5 y=10 return (x,y) # Set some things with the function. a,b=func() # See what happened. print ("a is", a,"\nb is", b,"\nx is", x,"\ny is", y) ``` Gives me: ``` a is 5 b is 10 x is 2 y is 3 ``` How how did you use your function to get the name and age?

OpenStudy (e.mccormick):

Argh... he ran away. Hehe. Saw @dmw_ocw online! Did some extra clarification, but by the time I posted you were gone. I hope that code shows where you have an issue. I think, but don't know, that you have something like that going on.

OpenStudy (dmw_ocw):

OK, so I was just not calling the function properly. In my start function I was not assigning name to get_info() I was just running get_info(). So just adding name = get_info() fixed it. Thanks everyone for getting me there.

OpenStudy (e.mccormick):

Yah, I though it was that.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!