0

I am just messing around with Python trying to do a simple guessing program. The code inside my if statement never runs, and I can't figure out why. I've tried printing both variables at the end, and even when they're the same, the comparison never resolves to true. I've also tried just setting y as 2 and guessing 2 as the input, and it still doesn't work.

import random

x = input("Guess a number 1 or 2: ")

y = random.randint(1,2)

if x==y:  
    print("yes")
betaclip
  • 3
  • 2

1 Answers1

2

The problem here is that x is a string and y is an int:

x = input("Try a number ")  # I chose 4 here

x
'4'

x == 4
False

int(x) == 4
True

input will always return a string, which you can convert to int using the int() literal function to convert that string into the required value

C.Nivs
  • 12,353
  • 2
  • 19
  • 44