How to take user input and substitute it into the solve method? I am trying to make a discord bot that helps you do math. Please assume that each code part has
from sympy import *
My code so far:
equation = input("Put your equation here:\n")
x, y, z, t = symbols('x y z t')
k, m, n = symbols('k m n', integer=True)
f, g, h = symbols('f g h', cls=Function)
equation = equation.split(",")
eqn = Eq(equation[0], equation[1])
await ctx.send(f"```{solve(eqn)}```")
The problem I'm getting:
SympifyError: SympifyError: 'x+1'
I know what I'm doing wrong, Sympy likes stuff like this:
eqn = Eq(x+1, 8)
solve(eqn)
This outputs 7. But how do I do it from user input?
EDIT: After doing a bit of research, I found that I needed to await returns.
So I awaited Eq(equation[0], equation[1]).
But then I get this error:
Ignoring exception in command solve: Traceback (most recent call last): File "C:\Users\justi_zts5a0w\PycharmProjects\discord.py\venv\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "math.py", line 42, in solve eqn = await Eq(parse_expr(equation[0]), parse_expr(equation[1]))
TypeError: object Equality can't be used in 'await' expression
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\justi_zts5a0w\PycharmProjects\discord.py\venv\lib\site-packages\discord\ext\commands\bot.py", line 903, in invoke await ctx.command.invoke(ctx)
File "C:\Users\justi_zts5a0w\PycharmProjects\discord.py\venv\lib\site-packages\discord\ext\commands\core.py", line 859, in invoke await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\justi_zts5a0w\PycharmProjects\discord.py\venv\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped raise CommandInvokeError(exc) from exc discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: object Equality can't be used in 'await' expression
I figured out that the problem is with the solve() command. the Eq() command seems to work just fine.
What I tried to do:
@client.command()
async def solve(ctx, equation):
x, y, z, t = symbols('x y z t')
k, m, n = symbols('k m n', integer=True)
f, g, h = symbols('f g h', cls=Function)
equation = equation.split("=")
place_hold = 1
await ctx.send(f"```{await
solve(Eq(parse_expr(equation[0]),parse_expr(equation[1])))}```")
But then this gives me the error:
ret = await coro(*args, **kwargs)
File "math.py", line 43, in solve
await ctx.send(f"```{await solve(Eq(parse_expr(equation[0]),
parse_expr(equation[1])))}```")
File "C:\Users\justi_zts5a0w\PycharmProjects\discord.py\venv\lib\site-
packages\discord\ext\commands\core.py", line 374, in __call__
return await self.callback(*args, **kwargs)
TypeError: solve() missing 1 required positional argument: 'equation'
I know what the error means, and I suspected it had to do with something in Classes and OOP, the self variable is not filled in and it needs to be filled in. So then I tried putting a place holder before the actual equation, but then that gives me this:
Traceback (most recent call last):
File "C:\Users\justi_zts5a0w\PycharmProjects\discord.py\venv\lib\site-
packages\discord\ext\commands\bot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\justi_zts5a0w\PycharmProjects\discord.py\venv\lib\site-
packages\discord\ext\commands\core.py", line 859, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\justi_zts5a0w\PycharmProjects\discord.py\venv\lib\site-
packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception:
AttributeError: 'Equality' object has no attribute 'split'
From this, I don't know what to do. It says there is something wrong with the split, but I don't know why something is wrong with it.
Edit: On further inspection, when I printed the type of the variable equation, it showed:
<class 'str'>
<class 'sympy.core.relational.Equality'>
The first one shows that it is a string. But then it appears to change to sympy.core.relational.Equality. So now my problem is: How to split a 'sympy.core.relational.Equality' object?