Program that reads 6 EVEN integer values and shows on screen in reverse order

Asked

Viewed 53 times

-1

I’m training Python and I’m having trouble with this task:

Create a program that reads 6 even integer values and shows on the screen on reverse order.

My current code is:

lista = []

teste = 0

for i in range(6):

    print(f'Digite um número par: ', end=' ')
    teste = int(input())

    if teste % 2 == 0:
        lista.append(int(teste))

print(lista)

lista.sort()

lista.reverse()

print(lista)

Only it closes after the 6 times of for, even if I put odd numbers it will terminate after 6 attempts of the for. I wanted to make a loop until it has 6 even values in the variable lista.

  • 2

    One option is to use a while where the condition is that the array length is less than 6.

  • But isn’t it obvious? You put range(6). What I would suggest is to use a while and measure the len() array lista for it to exit the loop when the size reached 6.

  • This here teste = int(input()) is redundant here int(teste), nay?

  • Got it, thanks guys.

  • list = [] test = 0 while Len(list) < 6: print(f'Type an even number: ', end=' ') test = int(input()) if test % 2 == 0: list.append(test) print(list) list.Sort() list.Reverse() print(list)

No answers

Browser other questions tagged

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