Python:缩进错误

时间:2015-08-18 19:53:28

标签: python function python-2.7 indentation

使用Python我正在尝试Project Euler Problem#7,它要求找到第10001个素数,但是在运行我的代码时遇到错误

"File "<stdin>", line 3
    TestedValue = 2
    ^
IndentationError: unexpected indent
Unknown error."

我假设这不是唯一面临该错误的那条线,但这是第一个出现的错误,任何想法我做错了什么?谢谢!

代码:

def PrimeNum(NumIn):
    PrimeNumCounter = 0
    TestedValue = 2
    if TestedValue == 2:
        PrimeNumCounter += 1
        TestedValue += 1
    else: 
        while PrimeNumCounter != NumIn-1
            for i in range(2, TestedValue):
                Prelim = 0
                if TestedValue % i != 0:
                    Prelim += 1 
                elif TestedValue % i == 0:
                    Prelim = 0 
                if Prelim > 0:
                    PrimeNumCounter += 1
        if PrimeNumCounter == NumIn-1:
            for i in range(2, TestedValue):
                Prelim = 0
                if TestedValue % i != 0:
                        Prelim += 1 
                elif TestedValue % i == 0:
                    Prelim = 0 
            if Prelim > 0:
                print TestedValue

1 个答案:

答案 0 :(得分:3)

您的文本编辑器正在混合空格和缩进。 Python只能处理一个或另一个。

要修复,

  

使用在Tools / scripts /中找到的reindent.py脚本   Python安装目录:

     

更改Python(.py)文件以使用4空格缩进而不使用硬标签   字符。还可以从线的末端修剪多余的空格和标签   删除文件末尾的空行。还要确保最后一行结束   换行。查看该脚本以获取详细用法   指令。

来自How to fix python indentation

相关问题