How does my input not equal the answer?

Switching from Unity JS to Python for a bit, and some of the finer points elude me as to why this does not work. My best guess is that the variable guess is actually a string, so string 5 is not the same as integer 5? Is this what is happening and either way how does one go about fixing this.

import random
import operator

ops = {
    '+':operator.add,
    '-':operator.sub
}
def generateQuestion():
    x = random.randint(1, 10)
    y = random.randint(1, 10)
    op = random.choice(list(ops.keys()))
    a = ops.get(op)(x,y)
    print("What is {} {} {}?\n".format(x, op, y))
    return a

def askQuestion(a):
    guess = input("")
    if guess == a:
        print("Correct!")
    else:
        print("Wrong, the answer is",a)

askQuestion(generateQuestion())

Solution 1:

Yes, you are absolutely right that "5" is distinct from 5. You can convert 5 into a string by using str(5). An alternative would be to convert "5" into an integer by int("5") but that option can fail, so better handle the exception.

So, the change to your program could be e.g. the following:

if guess == str(a):

instead of:

if guess == a:

Another option would be to convert guess into an integer, as explained in the other answer.

EDIT: This only applies to Python versions 2.x:

However, you're using input(), not raw_input(). input() returns an integer if you type an integer (and fails if you type text that isn't a valid Python expression). I tested your program and it asked What is 4 - 2?; I typed 2 and it sait Correct! so I don't see what is your problem.

Have you noticed that if your program asks What is 9 - 4? you can type 9 - 4 and it says Correct!? That's due to you using input(), not raw_input(). Similarly, if you type e.g. c, your program fails with NameError

I would however use raw_input() and then compare the answer to str(correct_answer)

Solution 2:

I am assuming you are using python3.

The only problem with your code is that the value you get from input() is a string and not a integer. So you need to convert that.

string_input = input('Question?')
try:
    integer_input = int(string_input)
except ValueError:
    print('Please enter a valid number')

Now you have the input as a integer and you can compare it to a

Edited Code:

import random
import operator

ops = {
    '+':operator.add,
    '-':operator.sub
}
def generateQuestion():
    x = random.randint(1, 10)
    y = random.randint(1, 10)
    op = random.choice(list(ops.keys()))
    a = ops.get(op)(x,y)
    print("What is {} {} {}?\n".format(x, op, y))
    return a

def askQuestion(a):
    # you get the user input, it will be a string. eg: "5"
    guess = input("")
    # now you need to get the integer
    # the user can input everything but we cant convert everything to an integer so we use a try/except
    try:
        integer_input = int(guess)
    except ValueError:
        # if the user input was "this is a text" it would not be a valid number so the exception part is executed
        print('Please enter a valid number')
        # if the code in a function comes to a return it will end the function
        return
    if integer_input == a:
        print("Correct!")
    else:
        print("Wrong, the answer is",a)

askQuestion(generateQuestion())