Python解析时出现意外的EOF(python2.7)

Python解析时出现意外的EOF(python2.7),python,python-2.7,Python,Python 2.7,这是我的python代码。有人能告诉我有什么问题吗? 我试着学习一个解24分游戏的算法。 但我真的不知道为什么这个程序会出错 from __future__ import division import itertools as it __author__ = 'linchen' fmtList=["((%d%s%d)%s%d)%s%d", "(%d%s%d)%s(%d%s%d)", "(%d%s(%d%s%d))%s%d", "%d%s((%d%s%d)%s%d)", "(%d%s(%d%

这是我的python代码。有人能告诉我有什么问题吗? 我试着学习一个解24分游戏的算法。 但我真的不知道为什么这个程序会出错

from __future__ import division
import itertools as it
__author__ = 'linchen'

fmtList=["((%d%s%d)%s%d)%s%d", "(%d%s%d)%s(%d%s%d)", 
"(%d%s(%d%s%d))%s%d", "%d%s((%d%s%d)%s%d)", "(%d%s(%d%s(%d%s%d))"]
opList=it.product(["+", "-", "*", "/"], repeat=3)


def ok(fmt, nums, ops):
    a, b, c, d=nums
    op1, op2, op3=ops
    expr=fmt % (a, op1, b, op2, c, op3, d)
    try:
        res=eval(expr)
    except ZeroDivisionError:
        return
    if 23.999< res < 24.001:
        print expr, "=24"

def calc24(numlist):
    [[ok(fmt, numlist, op) for fmt in fmtList] for op in opList]


for i in set(it.permutations([3,3,8,8])):
    calc24(i)
来自未来进口部的

按原样导入itertools
__作者:林晨
fmtList=[((%d%s%d)%s%d)%s%d”,“(%d%s%d)%s(%d%s%d)”,
“(%d%s(%d%s%d))%s%d”,%d%s((%d%s%d)%s%d)”,“(%d%s(%d%s(%d%s%d))”]
opList=it.product([“+”、“-”、“*”、“/”],repeat=3)
def正常(fmt、nums、ops):
a、 b,c,d=nums
op1,op2,op3=ops
expr=fmt%(a,op1,b,op2,c,op3,d)
尝试:
res=评估(expr)
除零误差外:
回来
如果23.999
下面是发生的情况:

Traceback (most recent call last):
  File "D:\Program Files\JetBrains\PyCharm 4.0.5\helpers\pydev\pydevd.py", line 2217, in <module>
    globals = debugger.run(setup['file'], None, None)
  File "D:\Program Files\JetBrains\PyCharm 4.0.5\helpers\pydev\pydevd.py", line 1643, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:/Users/linchen/PycharmProjects/untitled/calc24.py", line 26, in <module>
    calc24(i)
  File "C:/Users/linchen/PycharmProjects/untitled/calc24.py", line 22, in calc24
    [[ok(fmt, numlist, op) for fmt in fmtList] for op in opList]
  File "C:/Users/linchen/PycharmProjects/untitled/calc24.py", line 15, in ok
    res=eval(expr)
  File "<string>", line 1
    (8+(3+(3+8))
               ^
SyntaxError: unexpected EOF while parsing
回溯(最近一次呼叫最后一次):
文件“D:\Program Files\JetBrains\PyCharm 4.0.5\helpers\pydev\pydevd.py”,第2217行,在
globals=debugger.run(安装程序['file'],无,无)
文件“D:\Program Files\JetBrains\PyCharm 4.0.5\helpers\pydev\pydevd.py”,第1643行,正在运行
pydev_imports.execfile(文件、全局、局部)#执行脚本
文件“C:/Users/linchen/PycharmProjects/untitled/calc24.py”,第26行,in
calc24(i)
文件“C:/Users/linchen/PycharmProjects/untitled/calc24.py”,第22行,在calc24中
[[fmt列表中的fmt的ok(fmt,numlist,op)]操作列表中的op]
文件“C:/Users/linchen/PycharmProjects/untitled/calc24.py”,第15行,ok
res=评估(expr)
文件“”,第1行
(8+(3+(3+8))
^
SyntaxError:分析时出现意外的EOF

有人能告诉我如何解决这个问题吗?

您是最后一个
fmtList
项目有不平衡的括号:

"(%d%s(%d%s(%d%s%d))"
应该是:

"(%d%s(%d%s(%d%s%d)))"

这就解释了回溯的原因——Python正在寻找一个结束的parethesis——相反,它遇到了和行尾(当使用
eval
时,行尾被解释为“文件结束”或EOF)因此,您得到了错误。

您错过了一个右括号。请使用编辑器或IDE来警告您这类事情。我在编码时疏忽了……谢谢您的建议