• anonymous
In Python, how do you reference a variable inside another function, or how do you make a local variable global? Here's my code: I'm attempting to find the first 1000 prime numbers.
Computer Science
  • Stacey Warren - Expert
Hey! We 've verified this expert answer for you, click below to unlock the details :)
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.
  • schrodinger
I got my questions answered at in under 10 minutes. Go to now for free help!
  • carlsmith
I'm a bit busy at the moment, but will be able to help you out soon if you're still stuck. You can always reference a global variable within a function, but to change it, you have to do global variable_name at the top of you function def a = 1 def func(): global a a += 1 I'll be on in a little while, let me know if you're still stuck on it.
  • carlsmith
P.S. You don't need to assign to `a` before the def, just before you call it. def func() global a a += 1 a = 1 func()
  • carlsmith
I'm not sure you want to nest function definitions like that. Also, if `odd` is a tuple, you can't mutate it. Try having a look through this, it find's the 1000th prime, but doesn't keep a list of all one thousand. You could easily get it to do that. Let me know if you're still stuck.

Looking for something else?

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