-2

I want to create a simple program which asks user the square of random number, and if the user inputs correct answer then it will show 'Good". But as you can see here, it is showing 'Error'. Why?

My code:

import random
a = random.randint(2, 10)
print(a)
b = a * a
print(b)
num = input("Enter answer\n")
if num == b:
    print("Good")
else:
    print("Error")
pjs
  • 18,696
  • 4
  • 27
  • 56
pumpkin
  • 1
  • 1

1 Answers1

1

As @KumarShivamRay explained you :

Input give the user input as a string, just use if int(num) == b:

here modified code explaining it:

import random
a = random.randint(2, 10)
print(a)
b = a * a
print(b)
num = (input("Enter answer\n"))
print('type num :',type(num))
num = int(input("Enter answer\n"))  ### here you need to convert input to int type
print('type num :',type(num))
if num == b:
    print("Good")
else:
    print("Error")

pippo1980
  • 2,181
  • 3
  • 14
  • 30