Comparison between random number and typed number never says it’s right, even though the numbers are equal

Asked

Viewed 361 times

0

I’m trying to make a program in which I can type a number n and then I would like to generate a random number between 1 and 2. If that random number is the same as n, would show "You won!" and otherwise, "You lost". However, even when you hit the number, it is showing "You lost!".

Follow my code. Thank you.

from random import randint

n = (input('Digite um número: '))  
print (randint( 1, 2))  
if n == (randint):  
    print ('Você ganhou!')  
else:  
    print ('Você perdeu!')  

4 answers

1

I think this is what you want:

from random import randint
if int(input('Digite um número: ')) == randint( 1, 2):
    print ('Você ganhou!')
else:
    print ('Você perdeu!')

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Did not make the conversion of string for int in the data entry and did not save the random number generated in variable to test later. This code is still a little flawed, but it works most of the time, for now I think it is good, after all it is learning in a little structured way and does not go far like this.

1


When you do

if n == (randint):  

you are comparing the n with randint, which is a function and not the selected value (note that functions are objects in Python). This comparison will always be negative in your case.

Save the selected value in a variable to compare later. For example:

numero_sorteado = randint(1, 2)

And, at the time of comparison, stay tuned to the types. the function input() returns a string, then convert to a number with the function int before comparing:

n = int(input('Digite um número: '))  
numero_sorteado = randint(1, 2)
print(numero_sorteado)  
if n == numero_sorteado:  
    print ('Você ganhou!')  
else:  
    print ('Você perdeu!')  

0

Just complementing the other answers. You can also use one ternary operator to complete this routine of checking the number with the drawn number and simplify a little more the code, see below:

from random import randint

print("Você ganhou!") if int(input("Numero: ")) == randint(1, 2) else print("Você perdeu!")

The syntax of this operator is as follows

x if condicao else y

In the case of the condition int(input("Numero: ")) == randint(1, 2) be true to the instruction on the left side of the if will be executed, otherwise the instruction to be executed will be from the command else.

See working on repl it..

0

So, the way your code is the comparison made by the 'if' is basically a string (representation in text, in this case a number 1 or 2), with an integer (the number itself and not a representation of it):

if n == (randint)

With the detail that as there is a variable storing the first randint, you are basically running a method randint 'x'(generating an 'x' result), showing this method 'x' for the user, and then running a method 'randint' 'y', and using this for comparison with user input.

So I suggest storing randint in a variable of type int(integer), show this stored variable to the user and use it in comparison.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.