Program Problem - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Program Problem (/thread-18643.html) |
Program Problem - ChrisG - May-25-2019 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. RE: Program Problem - ichabod801 - May-25-2019 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: .
RE: Program Problem - ChrisG - May-25-2019 (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 Thank you so much! And the ''!='' in line 12 I forgot to change. I was only testing if that should work. But again, thanks! |