I am doing a maths quiz for my coursework based around differentiation and integration using python 3. I'm finding it difficult to check if the answer, typed in by the user, is correct. I'm currently using the code below. However, if the user types in answer but in a different order it is treated as incorrect. for example if the answer was x^5 + 6 but I typed in 6 + x^5 it would be treated as incorrect despite both answers being correct. How would I overcome this issue while keeping the answer in a string state?
if self.Answer.text() == FDiffAnswer[1]:
self.score = self.score + 1
else:
self.score = self.score + 0
Using sympy this can be done as follows...
x = symbols('x')
// user_input and original_eq are strings
expr1 = sympify(user_input) // user input
expr2 = sympify(original_eq) // Answer
print expr1==expr2
Double equals signs (==) are used to test equality. However, this tests expressions exactly, not symbolically
http://docs.sympy.org/dev/gotchas.html#double-equals-signs
But this returns True for all the following cases:
x**6 + 1 == x**(6) + 1
x**6 + 1 == 1 + x**6
x**3 + 1 == x*x*x + 1
PS: Sympy(and Python) uses the symbol ^ for XOR operation. So if you want it to be used as power operation you need to replace it by ** before calling sympify
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With