开发者

Python - New beginner question, invalid syntax from a example file

开发者 https://www.devze.com 2023-04-01 06:13 出处:网络
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:

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 a开发者_运维问答s 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!


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消