编译单个语句时发现多个语句 [英] Multiple statements found while compiling a single statement

查看:72
本文介绍了编译单个语句时发现多个语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

感谢社区提供的任何帮助.谢谢!

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

代码如下:

<预><代码>>>>'''将文件大小转换为人类可读的形式.可用功能:近似大小(大小,a_kilobyte_is_1024_bytes)获取文件大小并返回一个人类可读的字符串例子:>>>近似大小(1024)'1.0 KiB'>>>近似大小(1000,假)'1.0 KB''''后缀 = {1000: ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],1024:['KiB'、'MiB'、'GiB'、'TiB'、'PiB'、'EiB'、'ZiB'、'YiB']}定义近似大小(大小,a_kilobyte_is_1024_bytes=True):'''将文件大小转换为人类可读的形式.关键字参数:size -- 文件大小(以字节为单位)a_kilobyte_is_1024_bytes -- 如果为 True(默认),则使用 1024 的倍数如果为 False,则使用 1000 的倍数返回:字符串'''如果尺寸<0:raise ValueError('数字必须是非负数')倍数 = 1024 如果 a_kilobyte_is_1024_bytes 否则 1000对于 SUFFIXES[multiple] 中的后缀:大小/= 倍数如果尺寸<多:返回 '​​{0:.1f} {1}'.format(size, suffix)引发 ValueError('数字太大')如果 __name__ == '__main__':打印(近似大小(1000000000000,假))打印(近似大小(1000000000000))**语法错误:编译单个语句时发现多个语句**>>>

解决方案

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

if (False):打印(你好")打印(世界")

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

if (False):打印(你好")打印(世界")

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

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

def foo():如果是真的):打印(你好,世界")

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

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

 a="Hello, World"如果是真的):打印(一)

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

a="你好,世界"如果是真的):打印(一)

将打印Hello, World".

现在专注于您的代码:

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

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

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

还有:

 defroximate_size(size, a_kilobyte_is_1024_bytes=True):

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

def 近似大小(大小,a_kilobyte_is_1024_bytes=True):

还有:

如果 __name__ == '__main__':打印(近似大小(1000000000000,假))打印(近似大小(1000000000000))

必须:

如果 __name__ == '__main__':打印(近似大小(1000000000000,假))打印(近似大小(1000000000000))

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

SUFFIXES = {1000: ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],1024:['KiB'、'MiB'、'GiB'、'TiB'、'PiB'、'EiB'、'ZiB'、'YiB']}定义近似大小(大小,a_kilobyte_is_1024_bytes=True):'''将文件大小转换为人类可读的形式.关键字参数:size -- 文件大小(以字节为单位)a_kilobyte_is_1024_bytes -- 如果为 True(默认),则使用 1024 的倍数如果为 False,则使用 1000 的倍数返回:字符串'''如果尺寸<0:raise ValueError('数字必须是非负数')倍数 = 1024 如果 a_kilobyte_is_1024_bytes 否则 1000对于 SUFFIXES[multiple] 中的后缀:大小/= 倍数如果尺寸<多:返回 '​​{0:.1f} {1}'.format(size, suffix)引发 ValueError('数字太大')如果 __name__ == '__main__':打印(近似大小(1000000000000,假))打印(近似大小(1000000000000))

我希望这会有所帮助!

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."

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.

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.

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.

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

解决方案

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:

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

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".

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:

    a="Hello, World"

if(True):
    print(a)

Will give you a syntax or indentation error, 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!

这篇关于编译单个语句时发现多个语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆