python - 与 sympy/pyqt python 的集成和区分

标签 python math sympy

我正在创建一个系统,使用 python 3 对用户进行集成和区分测试。当我显示问题时,它们的形式如下:

-25*x**(3/5)/3 + 6*x**(4/3) - 5*x**6/3 + x**2/2 - 4*x

我怎样才能把它改成这样的形式:

-25x^(3/5)/3 + 6x^(4/3) - 5x^6/3 + x^2/2 - 4x

我也想要它,这样当用户输入等效答案时它仍然可以被识别

最佳答案

对于简单的显示器更换,您可以使用:

def format_math(string):
    return (string.replace("**", "^")).replace("*", "")

然后您可以将其与用户输入进行比较,以将他们的输入答案与您的答案进行比较。

 x = format_math("-25*x**(3/5)/3 + 6*x**(4/3) - 5*x**6/3 + x**2/2 - 4*x")
 # -25x^(3/5)/3 + 6x^(4/3) - 5x^6/3 + x^2/2 - 4x
 user_input = format_math(input("Enter your answer: "))
 # If the user enters # -25x^(3/5)/3 + 6x^(4/3) - 5x^6/3 + x^2/2 - 4x or
 # -25*x**(3/5)/3 + 6*x**(4/3) - 5*x**6/3 + x**2/2 - 4*x the program will 
 # recognize both as correct
 if x == user_input:
     return True

来自python docs:

关于python - 与 sympy/pyqt python 的集成和区分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28682283/

相关文章:

Python 链表搜索

python - 从单词和词性生成多级词典

java - 如何在Java中计算正态分布下的面积?

python - 对表达式执行简化时出现关键错误

python - sympy错误 'Symbol'对象不可调用

python - 是否可以使用 XGBoost 预测给定输入向量或一系列向量的整个输出向量?

python - 网站加载时如何检查django项目中的用户IP?

ios - 以编程方式绘制 iOS 7 风格的圆圈

java - Java中是否有常用的有理数库?

python-3.x - 在 python sympy 中,如何将外部 python 函数应用于 sympy 矩阵的每个元素?