dmw_ocw
  • 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?
MIT 6.00 Intro Computer Science (OCW)
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
katieb
  • katieb
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
anonymous
  • anonymous
@dmw_ocw If the syntax is correct for the differnet type, Yes. \(\color{red}{Chance}\)
dmw_ocw
  • 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
anonymous
  • anonymous
You should be able to find the error, it is pretty simple error in the syntax. \(\color{blue}{Chance}\)

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

e.mccormick
  • 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.
e.mccormick
  • 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.
e.mccormick
  • 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?
e.mccormick
  • 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.
dmw_ocw
  • 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.
e.mccormick
  • e.mccormick
Yah, I though it was that.

Looking for something else?

Not the answer you are looking for? Search for more explanations.