-3

any reason why this doesn't work?

import random



answer = random.randint(2, 4)
print(answer)

txt = input("what number")
if answer == txt:
    print("correct")

every time I enter the correct answer, it just comes back empty (or as an else statement if I put it) I had this working last night although now I cannot work out why it won't, PS. i've just started to learn python this week so please go easy on me

  • 2
    You are comparing integer against string, e.g. `"1" == 1` is False, you have to convert input to int. – zamir Jan 24 '20 at 12:37
  • 2
    as @zamir commented, txt = int(input("what number")) should do the trick – Ezio Jan 24 '20 at 12:41

1 Answers1

1

you can not compare string with integer.

import random

answer = random.randint(2, 4)
print(answer)

txt  = int(input("what number"))
if answer == txt:
    print("correct")
Rohit Chauhan
  • 1,119
  • 1
  • 12
  • 30