0

When I run my code and guess the right number, the code doesn't work and says try again.
How do I fix it?

import random   
   
number = random.randint(1,10)

print("Please enter your number down below")

yourguess = input()


if number == yourguess:

  print("You guessed it") 

else:

  print("Try again")
user
  • 7,435
  • 3
  • 14
  • 44
wooproop
  • 23
  • 5
  • 2
    Welcome to Stack Overflow! It'd be great if you could change the title of your question to describe your problem. Also, note that you're working with random numbers here - how do you know you really guessed the right number? – user Jan 29 '21 at 21:10
  • 1
    You are comparing an int to a string. `if str(number) == yourguess:` – β.εηοιτ.βε Jan 29 '21 at 21:13

2 Answers2

2

You either need to compare strings, or compare numbers. I suggest turning the input into an integer like this:

import random   
   
number = random.randint(1,10)

yourguess = int(input("Please enter your number: "))


if number == yourguess:

  print("You guessed it") 

else:

  print("Try again")
Pentium1080Ti
  • 1,040
  • 1
  • 7
  • 16
0

Input defaults to string, you need to change it to int to be comparable. Other than that your code is fine.

import random   
   
number = random.randint(1,10)

print("Please enter your number down below")

yourguess = input()
yourguess = int(yourguess)

if number == yourguess:

  print("You guessed it") 

else:

  print("Try again")
WolVes
  • 1,286
  • 2
  • 19
  • 39