0

I am adding a simple print variable line to a program and it is giving me an IndentationError. Code works with the "print yes" line commented out as shown, but when I uncomment I get the error:

Error:

factors.append(yes)
^
IndentationError: unexpected indent

Code:

n = 1
x = int(raw_input("What is the number you would like largest prime factor of?"))
factors= []
checklist = []
primefactors = []
while n < (x+1)/2:
    if x % n == 0:
        yes = n
        #print yes
        factors.append(yes)
    if n % 1000 == 0:
        print "running n count = %s" % (n)
n += 1

for j in factors:
    checklist = [j]
    for i in range(2, j):
        if j % i == 0:
            checklist.append(j/i)
    if len(checklist) == 1:
        primefactors.append(checklist)

print "All factors of %s are %s" % (x,factors)
print "The prime factors are %s" % (primefactors)
print "The highest prime factor is %s" % (max(primefactors))
FinnPegler
  • 81
  • 9
  • You are mixing tabs and spaces. Don't do that. Stick to *one style*, preferably spaces *only*. Python expands tabs to the next 8th column, but it looks like you configured tabs to expand to the 4th column instead. – Martijn Pieters Nov 03 '16 at 11:53
  • That's it, working now, thanks. – FinnPegler Nov 03 '16 at 12:01

2 Answers2

0

Tabs and Spaces are considered different in Python. Make sure you indent either with 4 spaces or a tab, don't use them interchangeably in a single program.
EDIT: I found out. It's in here:

if x % n == 0:
yes = n
#print yes
factors.append(yes)

change this to:

if x % n == 0:
    yes = n
    print yes
    factors.append(yes)
Arindam
  • 163
  • 1
  • 13
0

Your if block is not indented. Change the if block:

if x % n == 0:
yes = n
#print yes
factors.append(yes)

change this to:

if x % n == 0:
    yes = n
    print yes
    factors.append(yes)
Arindam
  • 163
  • 1
  • 13
  • Sorry, that was a mistake in my pasting, it was indented in my program. I will edit the code. I also think I just found the reason, I was using tab instead of 4 spaces. I converted all tabs to four spaces and it is working. Could that have been the reason? – FinnPegler Nov 03 '16 at 11:59
  • Yes. Actually don't use them interchangeably. Either stick with space indentation or tab indentation, otherwise Python will raise an error. – Arindam Nov 03 '16 at 12:04