This is the code I have at the moment:
print "What equation would you like"
a = raw_input()
print "What's", a
b = input()
print a == b
I want to prompt a user to enter a maths equation, for example 3+4
.
I’ve been trying to convert this to Python 3 by using input(), however, since input()
would actually solve the equation, the next line underneath would print the answer to equation like this: What's 7
.
Since the idea is trying to solve the equation inputed for variable a
, the entire idea of the code is meaningless as they could literally type in 7
and the next line would print out True
because 7 == 7
.
Is there any way that I can type in an equation like 3+4
and actually output What's 3+4
without Python telling me that the end result is False
?
Oh and in regards to: Math operations from string, I actually did see that post before posting this question and what it looked like is basically getting python to output an answer to an equation which went entirely against what I was trying to achieve. Instead, I wanted python just to tell me whether the answer which I had typed in was correct or incorrect, hence true and false.
In regards to my previous post, it was simply meant to be about answering an equation which I manually put in the script. However, since I wanted to ask a user to type in a maths equation in the new script, the method used previously wouldn't work.
I also tried ast.literal_eval(), but I got this error...
Traceback (most recent call last):
File "/tmp/KANTL/Math.py", line 7, in <module>
print literal_eval(a) == literal_eval(b)
File "/usr/lib/python2.7/ast.py", line 80, in literal_eval
return _convert(node_or_string)
File "/usr/lib/python2.7/ast.py", line 79, in _convert
raise ValueError('malformed string')
ValueError: malformed string