为什么这个 sympy 函数返回一个空列表作为答案?

发布于 2025-01-13 01:49:56 字数 381 浏览 4 评论 0原文

我正在尝试用 python 求解 x 的方程。

x - 1 = 99

我有这段代码,应该可以解决这个问题。

该代码是我能找到的重现该问题的最少代码。

from sympy import Symbol,solve
x = Symbol('x',real=True)
eq = x-1==99
answer = solve(eq,x)
print(answer)

当我运行代码时,它返回这个。

[]

我正在寻找的答案是 100。

没有错误消息,并且我将 x 设置为实数,所以我不明白为什么它返回这个。

有人可以帮忙吗?

I am trying to solve this equation for x with python.

x - 1 = 99

I have this code, which should solve this.

This code is the minimum code I could find to reproduce the issue.

from sympy import Symbol,solve
x = Symbol('x',real=True)
eq = x-1==99
answer = solve(eq,x)
print(answer)

When I run the code, it returns this.

[]

The answer I am looking for is 100.

There are no error messages, and I have x set as a real number, so I do not understand why it returns this.

Could someone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃→可爱长大的 2025-01-20 01:49:56

也许这可以帮助?

from sympy import solveset
from sympy import Symbol, Eq
x = Symbol('x')
solveset(Eq(x-1, 99), x)

Maybe this can help?

from sympy import solveset
from sympy import Symbol, Eq
x = Symbol('x')
solveset(Eq(x-1, 99), x)
来世叙缘 2025-01-20 01:49:56

我对 SymPy 不太熟悉,但只是查看

因此,只需将 99 移至等号的另一侧:

eq = x-1 - 99

答案:[100]

I'm not very familiar with SymPy, but just looking at the docs on solving algebraic equations, it says "We suppose all equations are equaled to 0". Also, if you print eq from your code, you'll see that it's False, not an equation.

So simply move the 99 to the other side of the equals sign:

eq = x-1 - 99

Answer: [100]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文