Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Program Problem
#1
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.
Reply
#2
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:.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem: Once I cancel the process my program will start working! Hadad 0 1,631 Jul-24-2019, 04:09 PM
Last Post: Hadad
  Program Problem (2) ChrisG 2 2,358 May-25-2019, 05:11 PM
Last Post: ChrisG
  problem running program in VScode LavaCreeperKing 4 3,800 Mar-25-2018, 04:34 PM
Last Post: LavaCreeperKing
  Program not running (Overloading problem) anurag123 2 2,521 Feb-19-2018, 07:23 PM
Last Post: nilamo
  Vending Machine Program: Problem with variables icabero0225 2 8,476 Jun-08-2017, 11:04 AM
Last Post: buran
  Whats my python program's problem arman 13 8,728 Mar-13-2017, 08:47 PM
Last Post: metulburr
  problem with importing my program meems 1 3,759 Nov-27-2016, 01:19 AM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020