0

All Im trying to do is make n a random number, if the user gets it correct it outputs "You got it!" but no matter what is always displays as if I got it wrong, even when I answered n

import random

n = random.randint(1,2)
guess = input("1 or 2?")
if guess == n:
  print("You won!")
  • 2
    Does this answer your question? [Python input never equals an integer](https://stackoverflow.com/questions/39775273/python-input-never-equals-an-integer) – Yevhen Kuzmovych Feb 08 '21 at 14:35

1 Answers1

2

In python, "10" == 10 is False. You must compare the same type, like 10 == 10 or "10" == "10".

input() returns a str, when random.randint() returns an int, so guess == n will always be False.

This is an easy way to solve your issue :

guess = int(input("1 or 2?"))

or

n = str(random.randint(1, 2))

or

n = random.choice("12")
Dorian Turba
  • 3,260
  • 3
  • 23
  • 67