The Solution
The problem is in these two lines:
q3 = input("Answer: ")
correctAnswer = (q3a,q3operator,q3b)
q3
gets a string. correctAnswer
gets a tuple of strings. When you compare the two, they'll obviously never be equal.
The simplest way to solve this is to change lines to the following:
q3 = int(raw_input("Answer: ")) # guarantee q3 is treated as an integer
correctAnswer = eval(str(q3a)+q3operator+str(q3b)) # eval treats a string as Python command
This should solve your problem for you.
How it works
eval
converts a string into a Python expression and evaluates it - something like "5 + 3" gets converted into the actual expression 5 + 3 and evaluated to yield 8.
I had to convert q3a
and q3b
to strings so that I could use eval
on them - hence why I used str(q3a)
and str(q3b)
.
Note that raw_input
only works on Python 2.7 - input
is the right thing to use in Python 3.
eval
is also potentially dangerous if you can't control what you pass to it - it's good in this situation, but the fact you have to use it indicates that there's probably a much better way to write this program. :)
How you can improve your program
The key to writing good programs is to use the right language constructs for them.
In this case, you can improve your code by utilising two very powerful Python features: dictionaries and lambda functions. I'll leave you to spend some time reading on what they are and could do.
Here's an example of what your program could look like with these improvements (to keep it simple, let's say you only want simple addition and subtraction problems):
functions = {"plus": lambda x, y : x + y, "minus": lambda x, y: x - y}
operation = random.choice(functions.keys())
a,b = random.randint(1,10), random.randint(1,10)
answer = int(raw_input("What is {0} {1} {2}?".format(a,operation,b))
print("Correct!" if answer == functions[operation](a,b) else "Wrong!")
Note how much shorter, cleaner and easier it is. You can add arbitrary functions to functions
and guarantee the rest of the program works perfectly.
Moral: Use the right tool for the right job.