Python unexpected EOF while parsing (python2.7)

2024/11/15 9:45:36

Here's my python code. Could someone show me what's wrong with it? I try to learn an algorithm on solving 24 - point game. But I really don't know why this program has an error.

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=numsop1, op2, op3=opsexpr=fmt % (a, op1, b, op2, c, op3, d)try:res=eval(expr)except ZeroDivisionError:returnif 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)

And Here's what happens:

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 runpydev_imports.execfile(file, globals, locals)  # execute the scriptFile "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 okres=eval(expr)File "<string>", line 1(8+(3+(3+8))^
SyntaxError: unexpected EOF while parsing

Could anyone told me how to fix this problem?

Answer

You're last fmtList item has unbalanced parenthesis:

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

should be:

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

And that explains the traceback -- Python is looking for a closing parethesis -- instead it encounters and end of line (when using eval, the end of line is interpreted as "End Of File" or EOF) and so you get the error.

https://en.xdnf.cn/q/119332.html

Related Q&A

Call a function in repl without brackets

Would like to know if there is a way to call a function in python in repl just with the function name. $ python -i interace.py >>> load 834.png >>> sharpen >>> saverather tha…

how to work on a exist session in selenium with python?

I want to fill some field of a webpage and then send a request to it but this website has a very powerful login page to avoid sending requests for login from a robot so I cant log in with selenium bu…

how to find similarity between many strings and plot it

I have a xls file with one column and 10000 strings I want to do few things 1- make a heatmap or a cluster figure shows the similarity percentage between each string with another one.In order to find …

Python and Variable Scope

So I am recently new to Python, but I seem to be able to program some stuff and get it working. However Ive been trying to expand my knowledge of how things work in the language, and putting this simp…

How to check if element is orthogonally adjacent (next to) to existing elements?

Im trying to make a simple game where a building placed in a nested list must be next to another building. The problem I face is that if the building was placed at the sides, I cant use for loops to ch…

How to add new column(header) to a csv file from command line arguments

The output of the following code:-import datetime import csv file_name=sample.txt with open(file_name,rb) as f: reader = csv.reader(f,delimiter=",") …

Pattern matching and replacing in Python

Im trying to take a string that can be anything like "Hello here is a [URL]www.url.com[/URL] and its great." and be able to extract whatever is between [URL] and [/URL] and then modify the st…

In Python word search, searching diagonally, printing result of where word starts and ends

I have a friend of mine tutoring me in learning Python and he gave me this project where a user will read a word search into the program and the file includes a list of words that will be in the word s…

Selenium python : element not interactable

I am trying to scrape information from this website example website I need to get the version 2021 and search by code. Here is my code: from selenium import webdriver from selenium.webdriver.chrome.opt…

Date into matplotlib graph

How can I use a date from a Sqlite database on the x-axis to make a bar graph with matplotlib?If I convert the date to unix timestamp the graph works, but I would like to get something like this: http…