[英]Python - New beginner question, invalid syntax from a example file
I am trying to learn myself python, but I am already failing at the basics. 我正在尝试学习自己的python,但是我已经在基础知识上失败了。
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. 当我运行bigdigits.py 1351355时,出现错误,提示SyntaxError:无效的语法。 I thought it had something to do with the wrong path or something, I am using C:\\py3eg as the path for my programs.
我以为它与错误的路径有关,或者与之有关,我使用C:\\ py3eg作为程序路径。 When I copied it to the C:\\Python32 path, same thing happened.
当我将其复制到C:\\ Python32路径时,发生了同样的事情。 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. 这本书是针对Python 3编程的,并且我安装了Python版本3.2.1。
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+. 您的代码是2.6+和3+中的有效Python代码。 However, it yields the following error message under Python 2.5:
但是,它在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. 那是因为Python 2.5不知道
as
关键字。 In 2.5, you'd use a comma instead: 在2.5中,您可以使用逗号代替:
except ValueError, err:
Note that this will break Python 3.x compatibility. 请注意,这将破坏Python 3.x的兼容性。
Instead of applying these patches to make your code run with the age-old 2.5, you should switch to a newer Python release. 您应该切换到较新的Python版本,而不是应用这些补丁以使代码在古老的2.5版本上运行。
我使用的是Eclipse + Pydev,python v.3.2.3,您的代码在Eclipse和IDLE中均运行良好。关键是句子“ ValueValue as err除外:”中的单词“ as”不是您的关键字IDE,所以我认为您应该将python切换到更高版本。
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): 为了获得更好的可读性,请尝试使用以下语法来定义零到九(尽管最好将它们命名为“零”到“九”,甚至“ _0”到“ _9”-保存类的大写名称,而不是变量):
Zero = """\
***
* *
* *
* *
* *
* *
*** """.splitlines()
EDIT : Here's some split/zip/* magic to define your digits: 编辑 :这是一些split / zip / *魔术来定义您的数字:
digitparts = """\
*** | * | *** | ***** | * | ***** | * | ***** | *** | ****
* * | ** | * * | * | * | * | * | * | * * | * *
* *| * | * * | ** | * | **** | * | * | * * | * *
* *| * | * | * | * | * | **** | * | *** | ****
* *| * | * | * | ******| * | * * | * | * * | *
* * | * | * | * * | * | * * | * * | * | * * | *
*** | *** | ***** | ** | * | *** | **** | * | *** | * """.splitlines()
Digits = zip(*(s.split('|') for s in digitparts))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.