Get the input as raw input and compare it to the stringversion of the result:
from random import randint
x2 = (randint(0, 1000000))
y2 = (randint(0, 1000000))
ans2 = raw_input("2. Answer to " + str(x2) + "-" + str(y2))
if ans2 == str(x2 - y2):
print("Correct!")
CorrectAns = CorrectAns + 1
else:
print("Wrong!")
This way they can still input the calculation you presented them, but it wont be evaluated to a number anymore. By comparing str with str they must match the exact result and you get around try: except:
that you need to guards against textinputs if you convert the input to a number for comparison.
With conversion is more lenient:
from random import randint
CorrectAns = 0
x2 = (randint(0, 1000000))
y2 = (randint(0, 1000000))
ans2 = raw_input("2. Answer to " + str(x2) + "-" + str(y2)+"\n")
try:
ansNum = int(ans2)
if ansNum == x2 - y2:
print("Correct!")
CorrectAns = CorrectAns + 1
else:
print("Wrong!")
except ValueError: # catch conversion errors if other things than int inputted
print("Wrong")