A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 5 years ago
In Python, how do you reference a variable inside another function, or how do you make a local variable global? Here's my code:
http://pastie.org/1849782
I'm attempting to find the first 1000 prime numbers.
anonymous
 5 years ago
In Python, how do you reference a variable inside another function, or how do you make a local variable global? Here's my code: http://pastie.org/1849782 I'm attempting to find the first 1000 prime numbers.

This Question is Closed

carlsmith
 5 years ago
Best ResponseYou've already chosen the best response.0I'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
 5 years ago
Best ResponseYou've already chosen the best response.0P.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
 5 years ago
Best ResponseYou've already chosen the best response.0I'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.
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.