A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 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.

  • This Question is Closed
  1. carlsmith
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  2. carlsmith
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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()

  3. carlsmith
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

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

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

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
  • 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.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.