Python:缩进错误

标签 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

最佳答案

您的文本编辑器正在混合空格和缩进。 Python 只能处理其中之一。

要修复,

Use the reindent.py script that you find in the Tools/scripts/ directory of your Python installation:

Change Python (.py) files to use 4-space indents and no hard tab characters. Also trim excess spaces and tabs from ends of lines, and remove empty lines at the end of files. Also ensure the last line ends with a newline. Have a look at that script for detailed usage instructions.

来自How to fix python indentation

关于Python:缩进错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32081490/

相关文章:

python - Python 中的 XML 引用

python - 代码不起作用,期望程序打印,但没有打印任何内容

function - Golang - 将结构作为参数传递给函数

python - 忠实地保留已解析 XML 中的注释

python - 如何在Python中正确加载一组图像

python - 使用 Pexpect 通过命令行将 Python 与 Fortran 连接

Python 帮助 "from distutils.core import setup"

arrays - 尝试通过函数访问 bash 数组会导致 ${1[@]} : bad substitution

python - 在 Python 中序列化二进制数据

python - matplotlib 阴影线和填充直方图