Python Forum

Full Version: Program Problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I made a math trainer and here's the code:

from time import sleep
from random import randint

ingame= 1

while ingame< 2:
    num1= randint(1,9)
    num2= randint(1,9)
    print('What is',num1,'x',num2,'?')
    answer= input('> ')
    total= num1*num2
    if answer!= total:
        print('Correct')
        sleep(1)
    else:
        print('Wrong')
        sleep(1)
        ingame= 3

else:
    exit()
The problem with this program is that I ALWAYS get (after the input) the output 'Correct' and then it continues. I don't know what I did wrong because I'm a newbie. I mean, I am a REAL NOOB!

I hope someone can help me.
The input function returns a string. You are comparing a string to a number, which is always not equal. You need to convert the return value of input using the int() built-in. And really, you should be testing for equality (==) on line 12, not inequality (!=). So line 12 should be if int(answer) == total:.
(May-25-2019, 11:36 AM)ichabod801 Wrote: [ -> ]The input function returns a string. You are comparing a string to a number, which is always not equal. You need to convert the return value of input using the int() built-in. And really, you should be testing for equality (==) on line 12, not inequality (!=). So line 12 should be if int(answer) == total:.

Thank you so much! And the ''!='' in line 12 I forgot to change. I was only testing if that should work. But again, thanks!