我编写了一个python脚本,得到了一个“无效语法”错误。有人能帮我找出错误吗?谢谢。
我的代码之一:
class Mahjong():
mentsu_map = {}
def __init__(self):
if len(Mahjong.mentsu_map) == 0:
m = Mahjong.mentsu_map
m[0] = [0, 0]
tablefile = open("pretable.data", "r")
try:
for line in tablefile:
ls = map(int, line.split(' '))
m[ls[0]] = [ m[ls[1], m[ls[2]] ]
finally: # invalid syntax here
tablefile.close()
def shanten(self, ht):
pass我的错误输出是:
finally:
^
SyntaxError: invalid syntax发布于 2013-12-05 15:09:26
你忘了一个]
m[ls[0]] = [ m[ls[1]], m[ls[2]] ]而不是
m[ls[0]] = [ m[ls[1], m[ls[2]] ]发布于 2013-12-05 15:09:48
在这里,您缺少了一个结束方括号:)
添加:
m[ls[0]] = [ m[ls[1]], m[ls[2]] ]如果您使用的是+Python2.6,那么优化还可以使用with语句:
with open("pretable.data", "r") as tablefile:
for line in tablefile:
ls = map(int, line.split(' '))
m[ls[0]] = [ m[ls[1]], m[ls[2]] ]它将在成功执行代码块后自动关闭文件。
希望这能有所帮助!
https://stackoverflow.com/questions/20403442
复制相似问题