【问题标题】:How to take user input and substitute it into the solve method?如何获取用户输入并将其替换为求解方法?
【发布时间】:2020-12-18 02:08:58
【问题描述】:

如何获取用户输入并将其替换为求解方法?我正在尝试制作一个可以帮助您进行数学运算的不和谐机器人。 请假设每个代码部分都有

from sympy import *

到目前为止我的代码:

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)}```")

我遇到的问题:

 SympifyError: SympifyError: 'x+1'

我知道我做错了什么,Sympy 喜欢这样的东西:

eqn = Eq(x+1, 8)
solve(eqn)

这输出 7。但是我如何从用户输入中做到这一点?

编辑:经过一番研究,我发现我需要等待退货。

So I awaited Eq(equation[0], equation[1]).

然后我得到这个错误:

忽略命令中的异常解决: 回溯(最近一次通话最后): 文件“C:\Users\justi_zts5a0w\PycharmProjects\discord.py\venv\lib\site-packages\discord\ext\commands\core.py”,第 85 行,已包装

ret = await coro(*args, **kwargs)

文件“math.py”,第 42 行,求解 eqn = await Eq(parse_expr(equation[0]), parse_expr(equation[1]))

TypeError: object Equality can't be used in 'await' expression

上述异常是以下异常的直接原因:

Traceback(最近一次通话最后一次):

文件“C:\Users\justi_zts5a0w\PycharmProjects\discord.py\venv\lib\site-packages\discord\ext\commands\bot.py”,第 903 行,在调用中 等待 ctx.command.invoke(ctx)

文件“C:\Users\justi_zts5a0w\PycharmProjects\discord.py\venv\lib\site-packages\discord\ext\commands\core.py”,第 859 行,在调用中 等待注入(*ctx.args, **ctx.kwargs)

文件“C:\Users\justi_zts5a0w\PycharmProjects\discord.py\venv\lib\site-packages\discord\ext\commands\core.py”,第 94 行,已包装 从 exc 引发 CommandInvokeError(exc) discord.ext.commands.errors.CommandInvokeError: 命令引发异常: TypeError: object Equality can't be used in 'await' expression

我发现问题出在solve() 命令上。 Eq() 命令似乎工作得很好。

我想做什么:

@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])))}```") 

但这给了我错误:

    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'

我知道错误的含义,我怀疑它与 Classes 和 OOP 中的某些东西有关,self 变量没有填写,需要填写。所以我尝试在实际之前放置一个占位符等式,但这给了我这个:

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'

从此,我不知道该怎么办。说分裂有问题,但我不知道为什么有问题。

编辑: 进一步检查,当我打印变量方程的类型时,它显示:

<class 'str'>
<class 'sympy.core.relational.Equality'>

第一个显示它是一个字符串。但后来它似乎变成了 sympy.core.relational.Equality。所以现在我的问题是: 如何拆分 'sympy.core.relational.Equality' 对象?

【问题讨论】:

  • 你试过parse_expr功能吗? docs.sympy.org/latest/modules/…
  • 那么我要不要使用 parse_expr(equaton[0]) 来将 equation[0] 转换成格式?
  • 但现在我得到:'Command.__call__' 从未等待 ret = await coro(*args, **kwargs) RuntimeWarning: E​​nable tracemalloc to get the object allocation traceback 这可能与不和谐的东西有关如果您不知道该怎么做,那很好,但我希望有人能告诉我。
  • 我找到了这个:stackoverflow.com/questions/54088263/… 但我不知道如何等待协程'Command.__call__'

标签: python discord.py user-input sympy


【解决方案1】:

isympy 会话中使用sympify

In [7]: equation = input('>')
>1+x = 0

In [8]: equation = equation.split('=')

In [9]: alist = [sympify(expr) for expr in equation]

In [10]: if len(alist)==2:
    ...:     expr = Eq(alist[0],alist[1])
    ...: 

In [11]: expr
Out[11]: x + 1 = 0

In [12]: solve(expr)
Out[12]: [-1]

看起来sympify 可以处理字符串列表:

In [18]: equation
Out[18]: ['1+x ', ' 0']

In [19]: sympify(equation)
Out[19]: [x + 1, 0]

In [20]: Eq(*sympify(equation))
Out[20]: x + 1 = 0

或者parse_expr:

In [25]: [parse_expr(s) for s in equation]
Out[25]: [x + 1, 0]

split('=') 的这种用法绕过了可能使用 '=' 来表示等于的情况。 '2x' 将被翻译成 '2*x'。您的用户可能会使用需要翻译成sympy 兼容形式的其他表达式。不要跳过同情介绍中的陷阱。

【讨论】:

  • 但我尝试使用 parse_expr 但它给了我错误:'Command.__call__' 从未等待 ret = await coro(*args, **kwargs) RuntimeWarning: E​​nable tracemalloc to get the object allocation traceback I对于我的问题,在 cmets 下也说了这个。
  • 你是如何运行这个脚本的?该错误表明不是简单的python调用。我在 ipythonisympy 会话中运行我的代码,并使用 sympy 导入和设置。
  • 我也做了第一种方法,第二种方法对我不起作用,因为 like 没有做任何事情,我仍然收到错误 SympifyError: "x+1"
  • 哦,是的,我用 sympi 制作了一个不和谐的机器人,它给了我一个错误。抱歉没有及时回复。
  • 我不能帮你discord
猜你喜欢
  • 1970-01-01
  • 2015-10-03
  • 1970-01-01
  • 2023-01-17
  • 2023-03-19
  • 2019-07-12
  • 2021-08-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多