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 program runs in a loop jasserin 0 1,318 May-18-2024, 03:07 PM
Last Post: jasserin
  Problem: Once I cancel the process my program will start working! Hadad 0 2,422 Jul-24-2019, 04:09 PM
Last Post: Hadad
  Program Problem (2) ChrisG 2 3,618 May-25-2019, 05:11 PM
Last Post: ChrisG
  problem running program in VScode LavaCreeperKing 4 5,664 Mar-25-2018, 04:34 PM
Last Post: LavaCreeperKing
  Program not running (Overloading problem) anurag123 2 3,712 Feb-19-2018, 07:23 PM
Last Post: nilamo
  Vending Machine Program: Problem with variables icabero0225 2 9,969 Jun-08-2017, 11:04 AM
Last Post: buran
  Whats my python program's problem arman 13 12,412 Mar-13-2017, 08:47 PM
Last Post: metulburr
  problem with importing my program meems 1 5,088 Nov-27-2016, 01:19 AM
Last Post: micseydel

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.