Python 编译单个语句时发现多个语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18771573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:47:13  来源:igfitidea点击:

Multiple statements found while compiling a single statement

pythonsyntax-error

提问by Enturio

I am brand new to programming. I am using the Dive into Python book and am trying to run the first example, humansize.py. I have copied and pasted the code into Idle, the Python shell and keep coming up with the same syntax error: "multiple statements found while compiling a single statement."

我是编程的新手。我正在使用 Dive into Python 一书并尝试运行第一个示例 humansize.py。我已将代码复制并粘贴到 Idle(Python shell)中,并不断出现相同的语法错误:“在编译单个语句时发现多个语句。”

I am downloading the code into BBEdit and then copying and pasting it into Idle. I have looked online and people said it could be a tab versus space issue. But I've went through the code and it looks identical to the book, I've even deleted and reinserted 4 spaces in all the lines of code and I'm still getting the error.

我正在将代码下载到 BBEdit 中,然后将其复制并粘贴到 Idle 中。我在网上看过,人们说这可能是制表符与空格的问题。但是我已经检查了代码,它看起来与书中的代码相同,我什至在所有代码行中删除并重新插入了 4 个空格,但仍然出现错误。

It's frustrating because I am sure that it's a simple issue but I've done everything that I know of, (in terms of trying to research the problem) to get it to work. If it is a space versus tabs issue, do any of you know where I can go and learn how to do the process of copying and entering code into Idle properly? I am a TRUE beginner.

这令人沮丧,因为我确信这是一个简单的问题,但我已经做了我所知道的一切(在尝试研究问题方面)以使其正常工作。如果是空格与制表符的问题,你们中有人知道我可以去哪里学习如何正确地将代码复制和输入到 Idle 中吗?我是一个真正的初学者。

I'd appreciate any assistance from the community. Thank you!

我很感激社区的任何帮助。谢谢!

I am running a Mac OSX - V.10.7.5. I am using the latest version of the Dive into Python book and Python 3.3.

我正在运行 Mac OSX - V.10.7.5。我正在使用最新版本的 Dive into Python 书籍和 Python 3.3。

The code is below:

代码如下:

>>> '''Convert file sizes to human-readable form.

    Available functions:
    approximate_size(size, a_kilobyte_is_1024_bytes)
    takes a file size and returns a human-readable string

Examples:
>>> approximate_size(1024)
    '1.0 KiB'
>>> approximate_size(1000, False)
    '1.0 KB'

    '''

    SUFFIXES = {1000: ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
            1024: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']}

    def approximate_size(size, a_kilobyte_is_1024_bytes=True):
    '''Convert a file size to human-readable form.

    Keyword arguments:
    size -- file size in bytes
    a_kilobyte_is_1024_bytes -- if True (default), use multiples of 1024
                                if False, use multiples of 1000

    Returns: string

    '''
    if size < 0:
        raise ValueError('number must be non-negative')

    multiple = 1024 if a_kilobyte_is_1024_bytes else 1000
    for suffix in SUFFIXES[multiple]:
        size /= multiple
        if size < multiple:
            return '{0:.1f} {1}'.format(size, suffix)

    raise ValueError('number too large')

    if __name__ == '__main__':
    print(approximate_size(1000000000000, False))
    print(approximate_size(1000000000000))

**SyntaxError: multiple statements found while compiling a single statement**
>>> 

采纳答案by NeverForgetY2K

You have a few indentation problems. In python indentation is very important, because the interpreter uses indentation levels to decide how to group statements. for example:

您有一些缩进问题。在python中缩进非常重要,因为解释器使用缩进级别来决定如何对语句进行分组。例如:

if (False):
    print("Hello")
print("World")

The statement(s) grouped with the if (False) statement should never be ran because if (False) should never be true. But the example I gave you will still output "World". This is because the interpreter doesn't see the second print statement as being a part of the if statement. Now if I were to take the exact same code and indent the second print statement like so:

不应运行与 if (False) 语句分组的语句,因为 if (False) 不应为真。但是我给你的例子仍然会输出“World”。这是因为解释器没有将第二个打印语句视为 if 语句的一部分。现在,如果我采用完全相同的代码并像这样缩进第二个打印语句:

if (False):
    print("Hello")
    print("World")

The interpreter will see that both print statements are one indentation level deeper than the if statement and nothing will be outputted because if (False) is always false.

解释器将看到两个打印语句都比 if 语句更深一层缩进,并且不会输出任何内容,因为 if (False) 始终为 false。

The same indentation applies to function definitions. For example:

相同的缩进适用于函数定义。例如:

def foo():
    if(True):
        print("Hello, World")

Because the if statement is indented one level deeper that the definition of foo it is grouped with the foo function. So when you call the foo function it will output "Hello, World".

因为 if 语句比 foo 的定义更深一层缩进,所以它与 foo 函数组合在一起。所以当你调用 foo 函数时,它会输出“Hello, World”。

Now for variables. In your code you have the variable indented in one level. Which would be fine if it were a part of a function definition, if statement, for loop, etc. But since it isn't, it creates problems. Take the following for example:

现在是变量。在您的代码中,您将变量缩进一级。如果它是函数定义、if 语句、for 循环等的一部分,那会很好。但由于不是,它会产生问题。以以下为例:

    a="Hello, World"

if(True):
    print(a)

Will give you a syntax or indentation error, While:

会给你一个语法或缩进错误,While:

a="Hello, World"

if(True):
    print(a)

Will print "Hello, World".

将打印“你好,世界”。

Now to focus on your code:

现在专注于您的代码:

   SUFFIXES = {1000: ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
        1024: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']}

Needs to be un-indented one level to become:

需要取消缩进一级才能成为:

SUFFIXES = {1000: ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
        1024: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']}

Also:

还:

    def approximate_size(size, a_kilobyte_is_1024_bytes=True):

Needs to be un-indented one level to become:

需要取消缩进一级才能成为:

def approximate_size(size, a_kilobyte_is_1024_bytes=True):

And:

和:

if __name__ == '__main__':
print(approximate_size(1000000000000, False))
print(approximate_size(1000000000000))

Needs to be:

需要是:

if __name__ == '__main__':
    print(approximate_size(1000000000000, False))
    print(approximate_size(1000000000000))

Throw all of this together and you get:

把所有这些放在一起,你会得到:

SUFFIXES = {1000: ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
        1024: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']}

def approximate_size(size, a_kilobyte_is_1024_bytes=True):
    '''Convert a file size to human-readable form.

    Keyword arguments:
    size -- file size in bytes    
    a_kilobyte_is_1024_bytes -- if True (default), use multiples of 1024
                            if False, use multiples of 1000

    Returns: string

    '''
    if size < 0:
        raise ValueError('number must be non-negative')

    multiple = 1024 if a_kilobyte_is_1024_bytes else 1000
    for suffix in SUFFIXES[multiple]:
        size /= multiple
        if size < multiple:
            return '{0:.1f} {1}'.format(size, suffix)

    raise ValueError('number too large')

if __name__ == '__main__':
    print(approximate_size(1000000000000, False))
    print(approximate_size(1000000000000))

I hope this helps!

我希望这有帮助!