0

I am unsure what is wrong with my code. I am trying to write a program that finds the prime factorization of a number, and iterates through numbers. My code is

import math 
import time

def primfacfind(n1,n2):
    while n1 < n2:
        n = n1
        primfac=[]

        time_start = time.clock()

        def primes(n):
            sieve = [True] * n
            for i in xrange(3,int(n**0.5)+1,2):
                if sieve[i]:
                    sieve[i*i::2*i]=[False]*((n-i*i-1)/(2*i)+1)
            return [2] + [i for i in xrange(3,n,2) if sieve[i]]

        print primes(n)

        def factfind(lsp,n): #finds factors of n among primes
            for i in lsp:
                if n%i==0:
                    primfac.append(i)
                else:
                    i+=1

        factfind(primes(n),n)
        print primfac

        def simplify(lsp):
            for i in lsp:
                if lsp.count(i) > 1:
                    i+=1 #should simplify to 3^2 instead of 3,3; unfinished

        time_end = time.clock()
        time_elapsed = time_end - time_start
        print time_elapsed

        n1+=1

print primfacfind(6,15)

The error given is

Traceback (most recent call last):
  File "python", line 15
    sieve = [True] * n
        ^
IndentationError: expected an indented block

I've checked over my indentation again and again, and I am unsure as to what is wrong. The program worked when it was not in the overall function and while loop, but I don't see how that should make a difference. It would be appreciated if the answer code was as simple as possible to understand, as I am somewhat new to python.

Any help with this error would be appreciated. Thanks!

8
  • 5
    You're most likely mixing spaces and tabs. Get a text editor that can fix that Commented Oct 7, 2016 at 23:27
  • @MosesKoledoye, I'm using repl.it, an online compiler, and I've been hitting the tab key... Commented Oct 7, 2016 at 23:30
  • There might be an errant space somewhere. It's hard to tell. Is there a way to just use python on your computer? Text editors are the bees-knees. Also, although sometimes you want to define functions inside of other functions, in this case, they should be defined outside. That might also help with the indentation issues. Commented Oct 7, 2016 at 23:33
  • @juanpa.arrivillaga, okay, so define the functions outside, and then run the master function - okay, I'll try that. Thanks! Oh, and no, it's not possible for me to just use python on my computer. Commented Oct 7, 2016 at 23:33
  • 1
    @heather No worries. I'm actually voting to close becase "this question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers." Commented Oct 7, 2016 at 23:43

2 Answers 2

2

Download something like Sublime and highlight the code. Spaces will be dots and tabs will be dashes.

Sign up to request clarification or add additional context in comments.

Comments

-1

I put that code in my editor and it compiled just fine. So I went to line 12 where you have sieve = [True] * n and got rid of the indentation so it was indented the same as the line above it def primes(n): and I was able to recreate your error.

Perhaps try and add an additional indentation than you think. You can also try Enthought Canopy which is free if you go to a university if you want a different editor.

1 Comment

I would advise against Enthought Canopy, unless you really need to do statistical data analysis type stuff, because it is rather heavy-weight. This looks to be more intro-python stuff. A text editor and the command-line is all you need and it's good for beginners to learn that stuff anyway.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.