Pair or odd through Random module (Python)

Asked

Viewed 5,595 times

0

Recently I started learning programming language with python, I created a classic "game" to go learning with Random.randint for the user to try to guess which number was chosen by the machine.

I tried to do the same but this time to guess if the computer chose an EVEN or ODD number and I’m not quite understanding how to implement.

I created only in a simplified way and got the result, but I did not understand very well how to check if it is even or odd by the module Random

# Este é apenas o programa simples sem utilizar random    
numero = int(input('Digite um número qualquer: '))    
resultado = numero % 2 # Pega o resto do número    
if resultado == 0:
    print('O número {} é PAR'.format(numero))
else:
    print('O número {} é IMPAR'.format(numero))   

# O que eu pretendia fazer, mas ao invés de adivinhar o número, queria que o jogador adivinhasse se o número é par ou ímpar (Como não consegui implementar utilizei esse programa como exemplo)

import random    
from time import sleep    
r = random.randint(1,10)  # Computador gera um número aleatório de 1 a 10    

print('Pensei em um número entre 1 e 10.')
print('-=-' * 20)

user = int(input('Em que número pensei?: ')) #Jogador tenta adivinhar

print('PROCESSANDO...')
sleep(2) # Computador aguarda 2 segundos antes de continuar

if user == r:
    print('Você me venceu! eu pensei mesmo no número {}'.format(r))
else:
    print('Ganhei! O número que pensei foi {}, não {}'.format(r, user))
  • @Raphael What part are you struggling with? What is your specific question?

  • @LINQ Good afternoon, I’m not managing to implement a machine way to think of a number and then the user guess if that number chosen randomly by the computer is even or odd, more or less in the form of the number guessing game, but showing whether the computer chose an even or odd number. More or less like 'You made a mistake, the number I chose was odd' and the number chosen at random

  • @Raphael See if my answer clarifies you.

1 answer

1


You need to, instead of comparing the two numbers, compare the two modules.

Note that at the time of comparison, you have the following code

if user == r:

That will compare if the two numbers are equal, what we need here is to know if they have the same module.

Note also that I have not changed the messages of success or failure, still gives to understand that the user has hit exactly the number that the computer "thought", but this is detail.

import random    
from time import sleep    
r = random.randint(1,10)  # Computador gera um número aleatório de 1 a 10    

print('Pensei em um número entre 1 e 10.')
print('-=-' * 20)

user_input = int(input('Em que número pensei?: ')) #Jogador tenta adivinhar

print('PROCESSANDO...')
sleep(2) # Computador aguarda 2 segundos antes de continuar

if (user_input % 2) == (r % 2):
    print('Você me venceu! eu pensei mesmo no número {}'.format(r))
else:
    print('Ganhei! O número que pensei foi {}, não {}'.format(r, user_input))

See working on repl.it

  • Thank you very much LINQ! I had tested if with %2 in input and 'r' but I had forgotten to use the parentheses...hehe :x It was very well explained, thanks again!

  • But it also works without the parentheses. I put it so it doesn’t get confused. Anyway, good to have helped.

  • I must have tampered with something in the code at the time of the prints change and didn’t notice and must have affected the if. But anyway I was still not understanding very well the implementation of rest with Random, Brigadão!

Browser other questions tagged

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