Python - 新的初学者问题,示例文件中的语法无效
我正在尝试自学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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的代码是 2.6+ 和 3+ 中有效的 Python 代码。然而,在 Python 2.5 下它会产生以下错误消息:
那是因为 Python 2.5 不知道
as
关键字。在 2.5 中,您可以使用逗号来代替:请注意,这将破坏 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:
That is because Python 2.5 does not know the
as
keyword. In 2.5, you'd use a comma instead: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.
为了获得更好的可读性,请尝试使用以下语法来定义零到九(尽管这些最好命名为“零”到“九”,甚至“_0”到“_9” - 保存类的大写名称,而不是变量):
< strong>编辑:这里有一些 split/zip/* 魔法来定义你的数字:
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):
EDIT: Here's some split/zip/* magic to define your digits:
我使用的是 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.