IndentationError:意外缩进:打印命令导致问题

时间:2016-11-03 11:38:04

标签: python-2.7 primes

我正在向程序添加一个简单的打印变量行,它给了我一个IndentationError。代码使用“print yes”行注释掉,如图所示,但是当我取消注释时,我得到错误:

错误:

factors.append(yes)
^
IndentationError: unexpected indent

代码:

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

2 个答案:

答案 0 :(得分:0)

Python中的选项卡和空格被认为是不同的。确保缩进4个空格或制表符,不要在单个程序中互换使用它们 编辑:我发现了。它在这里:

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

将其更改为:

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

答案 1 :(得分:0)

您的if块没有缩进。更改if块:

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

将其更改为:

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