0
class Sieve:
def __init__(self, digit):
    self.digit = []
    numbers = [True]*digit
    if digit <= -1:
        raise RuntimeError("Cannot use negative values.")
    numbers[0] = False
    numbers[1] = False

def findPrimes(self):
    for i in range(len(self.digit)):
        if numbers[i]:
            j = (i+i)
            while (j < len(numbers)):
                numbers[j] = False
                j += i
                print(numbers)

This is my first post on stackoverflow and after trying for a long time figuring out what to do and searching the internet, I'm afraid I have to ask here. My program is supposed to find all prime numbers given any number in the initial class. I was trying to check my findPrimes(), but I was having a difficult time trying to get the values to print. I keep getting the error "numbers is not defined". Am I supposed to define it outside of findPrimes? Or is there a specific way I can call numbers from init to findPrimes? Thank you!

1 Answers1

0

According to your post, your functions are unindented, so they're defined outside of your class.

Remember python relies heavily on indenting due to the lack of things like braces.

Also, your variable are local to your functions, and aren't properties of your class. You need to add them to your class by prepending them with self. (like you did with self.digit).

so self.numbers = ... instead of numbers = ...

  • Ahh, I thought that too, but I wasn't sure if I could have two selfs on one function. As for the indents, on my program it is properly indented. I'm going to try to implement the self.numbers and recall it in the findPrimes function like you said. I'll let you know how it turns out, thanks for your help! – Hoang Nguyen Oct 04 '16 at 05:05