'Issue with "if" statement in combination with input and random numbers
I started today playing a little bit with python, so I'm really new.
I wanted to make a quiz for my little brother, that will generate multiplication functions of random numbers between 0 and 10. Everything went fine, except one thing- when I run the code it always returns false, even when I write a correct input.
I think the main problem is in the "if" statment, and I think the problem is that my input is not really equal to the "multiplication", but I dont know how to fix it.
I can't understand why the "one" is not equal to the "multiplication", I though the "multiplication" is basically the answer to the multiplication of the two random numbers, and if my input is correct it should be equal and print "true".
I would really like to help, I've been breaking my head over it for couple of hours now. Thanks!
import random
import sys
import os
random_num1 = random.randrange(0, 10)
random_num2 = random.randrange(0, 10)
print(random_num1, 'X', random_num2, '=', )
def multiplication(random_num1, random_num2):
sumNum = random_num1 * random_num2
return sumNum
one = input()
if(one == multiplication(random_num1, random_num2)):
print('true')
else:
print('false')
Solution 1:[1]
I don't know if you'd still need a 5 year late answer haha. However, here it is:
As I was looking at your code, I decided to remove the multiplication function, and just leave it as a normal action, because it isn't really needed at the moment. I also removed the other two imports, which weren't being used in the code.
Now, the real issue, was that the variable "one" was probably an integer, while the "sumNum" was probably a boolean. So, this is what I did, so it could compare two values of the same type:
if int(one) == int(sumNum):
print('true')
else:
print('false')
In the end, it Did work, and here is the full code:
import random
random_num1 = random.randrange(0, 10)
random_num2 = random.randrange(0, 10)
print(random_num1, 'X', random_num2, '=', )
sumNum = random_num1 * random_num2
one = input()
if int(one) == int(sumNum):
print('true')
else:
print('false')
Sorry if this response is too late, which probably is...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | 01aUser01 |
