-2

So I made a "Guess the Number" program with Python and tested it. Apparently, choosing the correct number still comes up as the incorrect else statement. How can I fix this?

As you can see the 3 I entered apparently isn't the same as the 3 my program came up:

Screenshot

finefoot
  • 9,914
  • 7
  • 59
  • 102
JuanR4140
  • 85
  • 6

2 Answers2

1

You're comparing the return value of your call to input (the string in your variable usernum) with the return value of random.randint which is an integer in your variable EasyRN.

You'll need to convert either the integer into an string:

usernum = int(usernum)

Or the string into an integer:

EasyRN = str(EasyRN)

Afterwards, you can use == to compare them.

finefoot
  • 9,914
  • 7
  • 59
  • 102
1

The result of input() is text (in Python, a str, short for the word "string" which is used in programming), while the output of random.randint() is a number (an int, short for "integer").

>>> type("3")
<class 'str'>
>>> type(3)
<class 'int'>

If you compare a str and an int they will never be equivalent, as it's an apples-to-oranges comparison.

Look at the int() function which converts a string to an integer.

>>> int("3")
3
>>> type(int("3"))
<class 'int'>
rgov
  • 3,516
  • 1
  • 31
  • 51