-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
.
One option is to use a
while
where the condition is that the array length is less than 6.– Luiz Felipe
But isn’t it obvious? You put
range(6)
. What I would suggest is to use awhile
and measure thelen()
arraylista
for it to exit the loop when the size reached6
.– Cmte Cardeal
This here
teste = int(input())
is redundant hereint(teste)
, nay?– Cmte Cardeal
Got it, thanks guys.
– Gabriel
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)
– Gabriel