0

I do not know why this is saying it is wrong when it isn't. I enter the correct answer and it says that is not correct the correct answer is what i inputted

import random
x = 1
score = 0
num = 1
while x == 1:
    a = random.randint(10, 201)
    b = random.randint(1, 201)
    a = int(a)
    b = int(b)
    c = a / 100 * b
    print ("What is")
    print (b)
    print ("% of")
    print (a)
    num = input()
    if c is num :
        print ("Well done!")
        score = score + 1
    elif c != num :
        print ("That is not correct, the correct answer is ", c)
  • 2
    Don't use `is` for such comparison, use `==`. Additionally `c` is a `float` due to the division so it may be slightly different from the exact integer value. – Michael Butscher Dec 28 '18 at 21:52

3 Answers3

5

There is a difference between '==' and 'is' in python.

'==' checks if the value is the same, while
'is' checks if they are the exact same object
See also: Is there a difference between `==` and `is` in Python?

Also, input() returns a string, but you compare it to a number. You have to convert the user input to a number first by using int(num) or float(num)

user8181134
  • 466
  • 2
  • 4
0

use of is comparison in this case is incorrect, is comparison is based on the object.

you should be using == as you need to compare actual values of the variables

so this

if c is num :

shouls be this

if (c == num):
Auxilus
  • 217
  • 2
  • 9
0
import random
x = 1
score = 0
num = 1
while x == 1:
    a = random.randint(10, 201)
    b = random.randint(1, 201)
    c = a / 100 * (b *1.0) # adding ‘* 1.0’ makes sure c is a float. 
    print ("What is %s\% of %s?" %(b, a)) # combined the prints in one line
    num = float(input()) # Change input to float
    if c == num :
        print ("Well done!")
        score = score + 1
    elif c != num :
        print ("That is not correct, the correct answer is ", c)
Niels Henkens
  • 2,553
  • 1
  • 12
  • 27