eval()
left と right は式ではなく文字列であるため、 s は機能しません。さらに、ユーザーが入力する可能性のあるものを有効な Python 式にするために、十分な検証を行う必要があります。得られる可能性のある値をいくつか考えてみましょう。
# The typical person's notation for x-squared isn't valid Python
x^2 + x - 3
# 3x means 3 * x to us, but to python it means nothing
3x + 4
# Some people use % to represent division, but that's a modulo operator to Python
3 % 4x
# Python doesn't understand the distributive property
3(4 - x)
# People might use some functions in a way Python doesn't understand
cos x
# Square brackets are used synonymously with parentheses
x[(x^2 - 5)(x^3 - 5x)]
要するに、プレーン テキストの数式を有効な Python 式に変換するには、何らかのエンジンが必要になります。これは簡単な作業ではありませんが、確実に実行できます。
あなたのプロジェクトは非常に野心的ですが、Python を使用する素晴らしい方法だと思います! 方程式を文字列として有効な Python に変換するインテリジェントな方法を思いついた場合は、すぐに共有する必要があります。科学および数学コミュニティのかなりの数の人々がそれを使用する可能性があるからです。