As I understand it, you are trying to create a little guessing game. Where the computer will draw a value between 1 and 6 and the user will try to discover the value drawn by the computer.
Well, to build the code, we must:
- Import the library Random, as well as the method randint;
- Draw a value between 1 and 6, storing it in a variable;
- Implement a repeat loop to capture the value typed by the user;
- Check if the user’s value corresponds to the drawn value;
- Display an approval or disapproval message.
With this logic and using Assignment Expressions, we can assemble the following code:
from random import randint
cont = 1
s = randint(1, 6)
while (n := int(input('Digite um valor entre "1" e "6": '))) != s:
print('Não foi desta vez. Tente novamente.')
cont += 1
print(f'Parabéns! Você acertou o valor "{s}" na {cont}º tentativa!')
Note that when we execute this code, the draw is carried out. Then, we are asked to enter a value. Later the code checks if the entered value corresponds to the drawn value. If so, we receive a positive message. If not, we will receive a disapproval message and then we will be prompted again for a value.
Lucas, I can apply this property while the variable n too?
– Felipe Aguiar
I didn’t understand why to do this separately, realize that while will run everything within its scope
– Lucas Miranda