Python - 新的初学者问题,示例文件中的语法无效

发布于 2024-12-02 08:55:17 字数 1666 浏览 0 评论 0原文

我正在尝试自学Python,但我的基础知识已经失败了。

我正在查看一本书中的一组示例,第一个示例是这样的:

import sys

Zero = ['  ***  ',' *   * ','*     *','*     *','*     *',' *   * ','  ***  ']
One = ['   *   ','  **   ','   *   ','   *   ','   *   ','   *   ','  ***  ']
Two = ['  ***  ',' *   * ',' *  *  ','   *   ','  *    ',' *     ',' ***** ']
Three = [' ***** ','     * ','   **  ','     * ','     * ',' *  *  ','  **   ']
Four = ['    *  ','   *   ','  *    ',' *     ',' ******','    *  ','    *  ']
Five = [' ***** ',' *     ',' ****  ','     * ','     * ',' *   * ','  ***  ']
Six = ['  *    ',' *     ',' *     ',' ****  ',' *   * ',' *   * ',' ****  ']
Seven = [' ***** ','     * ','    *  ','   *   ','  *    ',' *     ',' *     ']
Eight = ['  ***  ',' *   * ',' *   * ','  ***  ',' *   * ',' *   * ','  ***  ']
Nine = ['  **** ',' *   * ',' *   * ','  **** ','     * ','     * ','     * '] 

Digits = [Zero, One, Two, Three, Four, Five, Six, Seven, Eight, Nine]

try:
    digits = sys.argv[1]
    row = 0
    while row < 7:
        line = ''
        column = 0
        while column < len(digits):
            number = int(digits[column])
            digit = Digits[number]
            line += digit[row] + ' '
            column += 1
        print(line)
        row += 1
except IndexError:
    print('usage: bigdigits.py <number>')
except ValueError as err:
    print(err, 'in', digits)

当我运行 bigdigits.py 1351355 时,我收到一个错误,提示 SyntaxError: Invalid Syntax。我认为这与错误的路径或其他东西有关,我使用 C:\py3eg 作为我的程序的路径。当我将其复制到 C:\Python32 路径时,发生了同样的事情。我什至从书籍网站下载了代码,但仍然出现错误,所以代码显然没问题。

这本书是针对Python 3编程的,我安装了Python 3.2.1版本。

如果有人能指出我收到此错误的原因,我将不胜感激!

I am trying to learn myself python, but I am already failing at the basics.

I am going through a set of examples from a book, and the very first example was this:

import sys

Zero = ['  ***  ',' *   * ','*     *','*     *','*     *',' *   * ','  ***  ']
One = ['   *   ','  **   ','   *   ','   *   ','   *   ','   *   ','  ***  ']
Two = ['  ***  ',' *   * ',' *  *  ','   *   ','  *    ',' *     ',' ***** ']
Three = [' ***** ','     * ','   **  ','     * ','     * ',' *  *  ','  **   ']
Four = ['    *  ','   *   ','  *    ',' *     ',' ******','    *  ','    *  ']
Five = [' ***** ',' *     ',' ****  ','     * ','     * ',' *   * ','  ***  ']
Six = ['  *    ',' *     ',' *     ',' ****  ',' *   * ',' *   * ',' ****  ']
Seven = [' ***** ','     * ','    *  ','   *   ','  *    ',' *     ',' *     ']
Eight = ['  ***  ',' *   * ',' *   * ','  ***  ',' *   * ',' *   * ','  ***  ']
Nine = ['  **** ',' *   * ',' *   * ','  **** ','     * ','     * ','     * '] 

Digits = [Zero, One, Two, Three, Four, Five, Six, Seven, Eight, Nine]

try:
    digits = sys.argv[1]
    row = 0
    while row < 7:
        line = ''
        column = 0
        while column < len(digits):
            number = int(digits[column])
            digit = Digits[number]
            line += digit[row] + ' '
            column += 1
        print(line)
        row += 1
except IndexError:
    print('usage: bigdigits.py <number>')
except ValueError as err:
    print(err, 'in', digits)

When I ran bigdigits.py 1351355, I got an error, saying SyntaxError: Invalid Syntax. I thought it had something to do with the wrong path or something, I am using C:\py3eg as the path for my programs. When I copied it to the C:\Python32 path, same thing happened. I even downloaded the code from the books website, and still got the error, so the code is apparantly fine.

The book is for Python 3 programming, and that I have Python version 3.2.1 installed.

If anyone could point out the reason for me getting this error, I would be very grateful!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寄与心 2024-12-09 08:55:17

您的代码是 2.6+ 和 3+ 中有效的 Python 代码。然而,在 Python 2.5 下它会产生以下错误消息:

File "syntax-error.py", line 31
  except ValueError as err:
                     ^
SyntaxError: invalid syntax

那是因为 Python 2.5 不知道 as 关键字。在 2.5 中,您可以使用逗号来代替:

  except ValueError, err:

请注意,这将破坏 Python 3.x 的兼容性。

您应该切换到较新的 Python 版本,而不是应用这些补丁来让您的代码在旧的 2.5 上运行。

Your code is valid Python code in 2.6+ and 3+. However, it yields the following error message under Python 2.5:

File "syntax-error.py", line 31
  except ValueError as err:
                     ^
SyntaxError: invalid syntax

That is because Python 2.5 does not know the as keyword. In 2.5, you'd use a comma instead:

  except ValueError, err:

Note that this will break Python 3.x compatibility.

Instead of applying these patches to make your code run with the age-old 2.5, you should switch to a newer Python release.

七婞 2024-12-09 08:55:17

为了获得更好的可读性,请尝试使用以下语法来定义零到九(尽管这些最好命名为“零”到“九”,甚至“_0”到“_9” - 保存类的大写名称,而不是变量):

Zero = """\
  ***  
 *   * 
*     *
*     *
*     *
 *   * 
  ***  """.splitlines()

< strong>编辑:这里有一些 split/zip/* 魔法来定义你的数字:

digitparts = """\
  ***  |   *   |  ***  | ***** |    *  | ***** |  *    | ***** |  ***  |  **** 
 *   * |  **   | *   * |     * |   *   | *     | *     |     * | *   * | *   * 
*     *|   *   | *  *  |   **  |  *    | ****  | *     |    *  | *   * | *   * 
*     *|   *   |   *   |     * | *     |     * | ****  |   *   |  ***  |  **** 
*     *|   *   |  *    |     * | ******|     * | *   * |  *    | *   * |     * 
 *   * |   *   | *     | *  *  |    *  | *   * | *   * | *     | *   * |     * 
  ***  |  ***  | ***** |  **   |    *  |  ***  | ****  | *     |  ***  |     * """.splitlines()

Digits = zip(*(s.split('|') for s in digitparts))

For better readability, try this syntax for defining Zero thru Nine (although these would be better named as "zero" through "nine", or even "_0" thru "_9" - save the capitalized names for classes, not variables):

Zero = """\
  ***  
 *   * 
*     *
*     *
*     *
 *   * 
  ***  """.splitlines()

EDIT: Here's some split/zip/* magic to define your digits:

digitparts = """\
  ***  |   *   |  ***  | ***** |    *  | ***** |  *    | ***** |  ***  |  **** 
 *   * |  **   | *   * |     * |   *   | *     | *     |     * | *   * | *   * 
*     *|   *   | *  *  |   **  |  *    | ****  | *     |    *  | *   * | *   * 
*     *|   *   |   *   |     * | *     |     * | ****  |   *   |  ***  |  **** 
*     *|   *   |  *    |     * | ******|     * | *   * |  *    | *   * |     * 
 *   * |   *   | *     | *  *  |    *  | *   * | *   * | *     | *   * |     * 
  ***  |  ***  | ***** |  **   |    *  |  ***  | ****  | *     |  ***  |     * """.splitlines()

Digits = zip(*(s.split('|') for s in digitparts))
冰之心 2024-12-09 08:55:17

我使用的是 Eclipse + Pydev,python v.3.2.3,你的代码在 Eclipse 和 IDLE 中都运行良好。重点是“ except ValueError as err:”这句话中的“as”一词不是你的关键词IDE,所以我认为你应该将你的Python切换到更高版本。

I'm using Eclipse + Pydev,python v.3.2.3, your code is worked well in both Eclipse and IDLE.The point is the word "as" in the sentence "except ValueError as err:" not a key word in your IDE,so I think you should switch your python a higher version.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文