Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sympy
#1
Hello when I try the next code with sympy I can not cath the exeption of SyntaxError
from sympy.abc import x, y
	try:
		e =x + y + x)
#	except Sympifyerror
	except exception:
#	except SyntaxError as error:
#	except SyntaxError as ex:
        raise SyntaxError('Error during evaluation of sympy expression: '
                          + str(ex))
		print ("errores")
Thanks
Reply
#2
You cannot catch SyntaxError, since it is raised prior code execution (i.e. before try and except are executed); If you use eval, you can be able to catch it, e.g.

from sympy.abc import x, y
code = """
   e = x + y + x)
"""
try:
   eval(code)
except SyntaxError:
   print("Hello")
Reply
#3
But when I try I get:

Error:
psm@psm:~/Documentos$ python scriptsympy.py Traceback (most recent call last): File "scriptsympy.py", line 18, in <module> main() File "scriptsympy.py", line 8, in main eval(e =x + y + x) TypeError: eval() takes no keyword arguments
what's that mean?
Reply
#4
eval() cannot evaluate a statement with an equal sign in it. The interpreter is understanding "e = ..." as assigning the keyword argument "e" to the value of "x + y + x".

from sympy.abc import x, y
code = """
   x + y + x
"""
try:
   e = eval(code)
except SyntaxError:
   print("Hello")
Reply
#5
Hello again, new problems, when I run my code:
#!/usr/bin/python3.6
def main():
	from sympy.abc import x, y
	code = """
		x + y + x
	"""
	try:
		e=eval(code)
	except SyntaxError:
		print ("errores")
		print (e)
if __name__ == "__main__":
    main()
I've found the following mistake:
Error:
UnboundLocalError: local variable 'e' referenced before assignment
What I'm doing wrong?
Thanks
Reply
#6
Ok, exec would be more appropriate in this case.
As far as I understand, we want to raise SyntaxError intentionally, so why I left extra closing parenthesis.
So, variable assignment e =... or extra ) both causes SyntaxError, that is good.
Reply
#7
But what about the error message:
UnboundLocalError
Reply
#8
Since your code snippet is unformatted, I can just suppose what exactly code you ran.

def main():
    from sympy.abc import x, y
    code = """
    x + y + x
    """
    try:
        e = eval(code)
    except SyntaxError:
        print ("errores")
    print(e)

if __name__ == "__main__":
    main()
Error:
UnboundLocalError: local variable 'e' referenced before assignment
This is because the expression to be evaluated (the code string) includes spaces that causes
IndentationError. IndentationError, in turn, is subclass of SyntaxError, so, print(errors) is executed and variable e is not being assigned. This is why UnboundLocalError raised.
Reply
#9
Hello, I take out the sapaces:
def main():
	from sympy.abc import x, y
	code = """
		x+y+x
	"""
	try:
		e=eval(code)
	except SyntaxError:
		print ("errores")
		print (e)
if __name__ == "__main__":
    main()
And I have the same problem, how can I resolve my problem?
Error:
UnboundLocalError: local variable 'e' referenced before assignment
Reply
#10
def main():
    from sympy.abc import x, y
    code = """x+y+x"""
    e = None
    try:
        e = eval(code)
    except SyntaxError:
        print ("errores")
    if e is not None:
        print(e)

if __name__ == "__main__":
    main()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python sympy problem for symbolic equation definition joellapointe_engineering 0 84 Mar-24-2024, 11:09 PM
Last Post: joellapointe_engineering
  Sympy nonlinsolve does not return explicit solution axelle 1 2,193 Jan-20-2021, 11:38 AM
Last Post: Serafim
  plot differential equation sympy dan_adi 0 2,930 Oct-13-2020, 10:44 AM
Last Post: dan_adi
  Attribute error - Sympy VictorG8 1 4,893 Apr-09-2020, 06:03 PM
Last Post: snippsat
  How to use list of symbols for sympy calculation in python? tanmaybhise 1 2,756 Mar-01-2020, 10:36 PM
Last Post: Gribouillis
  Next zero of a function on sympy VicenteM 0 2,052 Aug-21-2019, 04:58 PM
Last Post: VicenteM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020