python 3.2 中出现 KeyError 语法错误
我是一个使用 python 3.2 的初学者,我有一本书的代码全部是 python 2.6 。我写了一个程序的一部分并不断得到: 语法错误:语法无效 然后 python 的 IDLE 突出显示我的代码中 KeyError 后面的逗号:
from tank import Tank
tanks = { "a":Tank("Alice"), "b":Tank("Bob"), "c":Tank("Carol")}
alive_tanks = len(tanks)
while alive_tanks > 1:
print
for tank_name in sorted( tanks.keys() ):
print (tank_name, tanks[tank_name])
first = raw_input("Who fires? ").lower()
second = raw_input("Who at? ").lower()
try:
first_tank = tanks[first]
second_tank = tanks[second]
except KeyError, name:
print ("No such tank exists!", name)
continue
I'm a beginner using python 3.2 and i have a book whos code is all in python 2.6. i wrote part of a program and keep getting:
Syntax Error: invalid syntax
Then python's IDLE highlights the comma after KeyError in my code:
from tank import Tank
tanks = { "a":Tank("Alice"), "b":Tank("Bob"), "c":Tank("Carol")}
alive_tanks = len(tanks)
while alive_tanks > 1:
print
for tank_name in sorted( tanks.keys() ):
print (tank_name, tanks[tank_name])
first = raw_input("Who fires? ").lower()
second = raw_input("Who at? ").lower()
try:
first_tank = tanks[first]
second_tank = tanks[second]
except KeyError, name:
print ("No such tank exists!", name)
continue
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
而不是
尝试
它是Python 2.x 和Python 3.x 之间的区别。不再支持第一种形式。
Instead of
try
Its a difference between Python 2.x and Python 3.x. The first form is no longer supported.