1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Development > not working in Python 2.6

Discussion in 'Software' started by TCoZ, 7 Jan 2011.

  1. TCoZ

    TCoZ Buffalo buffalo buffalo buffalo...

    Joined:
    3 May 2010
    Posts:
    277
    Likes Received:
    8
    I'm having a really weird problem. I have a loop where I use a variable, i, as a loop counter, and the loop stops when n > i is no longer true. However, when I run it, it doesn't stop. What am I doing wrong?

    Here's the offending code.

    Code:
    def returnPrimes(n): # Returns all the primes from 2 to n.
        i = 2 # Loop counter
        primeset = [] # Set of primes generated so far
        while n > i:
            print "Checking", i
            if isPrime(i, primeset):
                print i, "is prime!"
                primeset.append(i)
            raw_input("Press enter to continue.")
            i += 1
        return primeset
    Thanks.

    EDIT: Okay, I've changed it so it uses a for loop - for i in range(2,n): - and that's worked but I'm still curious about what I did wrong with the while loop.

    EDIT: Tried the original code again, and it started working. So, not really sure what went wrong there. Oh well.
     
    Last edited: 8 Jan 2011
  2. jaggyman

    jaggyman What's a Dremel?

    Joined:
    15 Jul 2010
    Posts:
    18
    Likes Received:
    0
    What does the standard output look like? The code looks fine but that line of

    i += 1

    looks like it's having no effect, is the indendation correct?
     

Share This Page