How to modify a repeat

Asked

Viewed 46 times

0

Follows the code:

numero = int(input('Digite um número:'))
msg1 = ('Errado')
msg2 = ('Tá osso em')
while numero != 0:
    print(msg1)
    numero = int(input('Digite um número:'))
print('Certo')

How do I make it so that after 10 mistakes, the msg2, without repeating?

3 answers

2

To do this, you need to have a variable that will count the number of times the user misses and a variable to get the new number the user entered, so that they do not lose the initial variable.

The following code snippet is used to confirm this numero the initial number, numero2 the number then entered in the loop and nErros the number of times the user erred in entering the correct number:

if(numero != numero2):
    nErros += 1

Then, so you can exit the loop, you can use the word break which automatically breaks the loop in which it is inserted. Knowing this, just check whether the user has already made a mistake x times, in this case, 10, so you can print the msg2 and the word break:

if(nErros >= 10):
    print(msg2)
    break
  • And then André, blz? If it was to interrupt msg1 only when it appeared at msg2, it would be with this break command somehow?

  • I’m sorry but I don’t understand your question, could you rephrase it, please? The break command is to get out of a loop, for, while or foreach

1

Hello, Glebson!

You can use a control variable and use one so that your loop is not interrupted unduly. As follows:

numero = int(input('Digite um número:'))
msg1 = ('Errado')
msg2 = ('Tá osso em!')
repeticao = 0
while numero != 0:
    repeticao += 1
    if repeticao == 10:
        print(msg2)
        numero = int(input('Digite um número:'))
    else:
        print(msg1)
        numero = int(input('Digite um número:'))
        pass
else:
    print('Certo')

.

Hug!

  • And then João blz? Massa Brow. That way the two appear. You can interrupt msg1 only when msg2 appears, then back with the first?

  • Blz, I adapted my first reply to print only one message or another. Opening

  • Thank you very much John

0

To perform this task, you need to create an auxiliary variable to count the number of laps the program has performed in the loop while, this way, you can verify when the amount of errors reaches 10.

But of course this is just not enough, as you will need to create a conditional to verify when the number reaches 10. You can then do the following:

numero = int(input('Digite um número:'))

msg1 = 'Errado'
msg2 = 'Tá osso em'
cont = 0

while numero != 0:

    if cont == 10:
        print(msg2)
    else:
        print(msg1)

    cont += 1
    numero = int(input('Digite um número:'))

print('Certo')

Now it becomes a question, do you need the message to appear only once or do you need it every 10 laps ? If it’s every 10 laps, change your parole to if cont % 10 == 0. Example:

while numero != 0:

    if cont % 10 == 0:  # A cada 10 voltas ele imprime a segunda mensagem.
        print(msg2)
    else:
        print(msg1)

If you need to get out of the loop while after the second message, you can use the declaration break to end the repeat.

while numero != 0:

    if cont == 10:
        print(msg2)
        break
    else:
        print(msg1)
  • Perfect man, cleared his mind, worth msm!

Browser other questions tagged

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