与sympy / pyqt python集成和区分

时间:2021-10-22 21:22:43

I'm creating a system which quizes the user on integration and differentiation using python 3. when i display questions they are in a form like:

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

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

How could I change it to a form like:

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

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

Also I want it so when users type in equivalent answers it is still recognised

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

2 个解决方案

#1


1  

For simple display replacement you could use:

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

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

Then you could use it versus user input to compare their input answer versus yours.

然后您可以使用它与用户输入来比较他们的输入答案与您的输入答案。

 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

From the python docs:

从python文档:

#2


1  

If the intention is to use sympy, then the following will work:

如果打算使用sympy,那么以下内容将起作用:

import sympy as sp
x = sp.symbols('x')
sp.init_printing()

y = -25*x**(sp.Integer(3)/sp.Integer(5)
    )/sp.Integer(3) + 6*x**(sp.Integer(4)/sp.Integer(3)
    ) - sp.Integer(5)*x**sp.Integer(6)/sp.Integer(3) + x**sp.Integer(
    2)/sp.Integer(2) - 4*x

y

与sympy / pyqt python集成和区分

Expressions can then be simplified and compared using sympy's tools. If it was preferable to not use sp.Integer() explicitly to prevent Python from doing the divisions, one could substitute it into the original expression string using regular expressions before using sp.sympify() to convert the string into a SymPy expression.

然后可以使用sympy的工具简化和比较表达式。如果最好不要显式地使用sp.Integer()来阻止Python进行分割,那么在使用sp.sympify()将字符串转换为SymPy表达式之前,可以使用正则表达式将其替换为原始表达式字符串。

#1


1  

For simple display replacement you could use:

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

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

Then you could use it versus user input to compare their input answer versus yours.

然后您可以使用它与用户输入来比较他们的输入答案与您的输入答案。

 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

From the python docs:

从python文档:

#2


1  

If the intention is to use sympy, then the following will work:

如果打算使用sympy,那么以下内容将起作用:

import sympy as sp
x = sp.symbols('x')
sp.init_printing()

y = -25*x**(sp.Integer(3)/sp.Integer(5)
    )/sp.Integer(3) + 6*x**(sp.Integer(4)/sp.Integer(3)
    ) - sp.Integer(5)*x**sp.Integer(6)/sp.Integer(3) + x**sp.Integer(
    2)/sp.Integer(2) - 4*x

y

与sympy / pyqt python集成和区分

Expressions can then be simplified and compared using sympy's tools. If it was preferable to not use sp.Integer() explicitly to prevent Python from doing the divisions, one could substitute it into the original expression string using regular expressions before using sp.sympify() to convert the string into a SymPy expression.

然后可以使用sympy的工具简化和比较表达式。如果最好不要显式地使用sp.Integer()来阻止Python进行分割,那么在使用sp.sympify()将字符串转换为SymPy表达式之前,可以使用正则表达式将其替换为原始表达式字符串。