Logic expression that controls repetition

Asked

Viewed 68 times

1

I’m solving a Python exercise that asks the user to give a guess, on what number the program got to pc randomly.

from random import randint
tent = int(input('PALPITE:'))
pc = randint(0,4)
cont = 0
while tent != pc:
    pc = randint(0,4)
    tent  = int(input('Você errou! o numero que o pc escoheu foi {}.  Tente novamente '.format(pc)))


print('Parabens!Você acertou! o Número que o o computador escolheu foi {}.'.format(pc))

The code until it works, but when I hit the number it says it’s wrong. When I put the input that’s inside the while to be running before (instead of randomizing the number first) the result works.

I know this has to do with the sequential structure, but I thought if I used the randint in the variable pc within the while would first give the same result.

Why does this happen? Does it have to do with the logical expression up there? It is mandatory that I have received the value of input inside the variable first and then Python parses?

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site

1 answer

2

The code is a little confusing and makes a mistake that I always see around. A beginner commits by not knowing and the expert commits because he thinks he can’t use any form of goto, then he gets mad at the break who does this and to avoid it, makes repetitive code that worsens the chance of good maintenance (not so much in simple code as this, but teaches the person to do wrong in more complex cases).

So I’m just going to ask the guess inside the loop, just once. And I’m going to separate the guess request from the error message. When wrong, the message appears and asks for the guess again. When right closes the loop and says it hit.

I am imagining that in each attempt another number should be drawn, if it should be the same then the draw should be out of the loop. It had an unused counter that I took, if it would us it even increases the chance that the draw should be unique and not change every attempt.

Simple and clean:

from random import randint

while True:
    pc = randint(0, 4)
    tent = int(input('PALPITE:'))
    if tent == pc:
        break
    print('Você errou! O número que o PC escolheu foi {}. Tente novamente '.format(pc))
print('Parabens! Você acertou! O número que o computador escolheu foi {}.'.format(pc))

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

With an accountant maybe it was like this:

from random import randint

pc = randint(0, 4)
contador = 0
while True:
    contador += 1
    tent = int(input('PALPITE:'))
    if tent == pc:
        break
    print('Você errou! Tente novamente '.format(pc))
print('Parabens! Você acertou em {} tentativas! O número que o computador escolheu foi {}.'.format(contador, pc))

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

Browser other questions tagged

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