#split the equation into 2 parts using the = sign as the divider, parse, and turn into an equation sympy can understand
equation = Eq(parse_expr(<input string>.split("=")[0]), parse_expr(<input string>.split("=")[1]))
answers = solve(equation)
#check for answers and send them if there are any
if answers.len == 0:
response = "There are no solutions!"
else:
response = "The answers are "
for answer in answers:
response = response + answer + ", "
response = response[:-2]
await self.client.send(response, message.channel)
我尝试制作一个使用sympy来解决代数问题的discord机器人,但是在实施过程中一直遇到错误。有人可以帮忙吗?
但是对于输入 10 = 2x
,parse_expr
会出现语法错误。我该如何使用parse_expr
或类似的函数来接受这种类型的表达式?
错误:
File "C:\Users\RealAwesomeness\Documents\Github\amber\amber\plugins/algebra.py", line 19, in respond
equation = Eq(parse_expr(c[2].split("=")[0]),parse_expr(c[2].split("=")[1]))
File "C:\Users\RealAwesomeness\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sympy\parsing\sympy_parser.py", line 1008, in parse_expr
return eval_expr(code, local_dict, global_dict)
File "C:\Users\RealAwesomeness\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sympy\parsing\sympy_parser.py", line 902, in eval_expr
expr = eval(
File "<string>", line 1
Integer (2 )Symbol ('x' )
^
SyntaxError: invalid syntax
sympy
的陷阱。由于 Python 的限制,即使有一个符号x
,2x
也是无效的,必须写成2*x
。 - hpaulj