Comparison between random number and typed number never says you got it right, even the numbers being equal

0

I'm trying to make a program where I can type a number n and then I would like to generate a random number between 1 and 2. If this random number is the same as n , it would show "You won!" and if not, "You have lost." But even when you hit the number, it's showing "You've lost!".

Here's 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!')  
    
asked by anonymous 15.02.2018 / 04:22

4 answers

0

When you do

if n == (randint):  

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

Save the value drawn in a variable so you can compare later. For example:

numero_sorteado = randint(1, 2)

And, at the time of the comparison, be aware of the types. the function input() returns a string, then convert it 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!')  
    
15.02.2018 / 05:38
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!')

You did not convert from string to int into data entry and did not save the generated random number into a variable to test later. This code is still a bit flawed, but it works most of the time, for an hour I think it's good, after all it's learning in an unstructured way and it does not go that far.

    
15.02.2018 / 05:06
0

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

if n == (randint)

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

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

    
15.02.2018 / 05:47
0

Just complementing the other answers. You can also use a ternary operator to complete this routine of verifying the number with the number raffled and simplify the code a little more, 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

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

See working at repl.it .

    
15.02.2018 / 16:18