For you to create a new lista
, containing the multiples of 3
, contained initially in another list, you can use the following algorithm...
meus_numeros = [1, 56, 342, 12, 781, 23, 43, 45, 123, 567]
multiplos_tres = list()
for c in meus_numeros:
if c % 3 == 0:
multiplos_tres.append(c)
multiplos_tres.sort()
print(f'\033[32mA lista dos múltiplos de "3" é: {multiplos_tres}')
Note that this algorithm scans the lista inicial
and checks that each of the elements are multiples of 3
. If so, I have entered multiplos_tres
.
After having inserted all possible multiples in multiplos_tres
, the algorithm sorts the values and then displays them.
Now, if you wish apenas exibir
the elements of the original list which are multiples of 3
, You can use the following algorithm...
meus_numeros = [1, 56, 342, 12, 781, 23, 43, 45, 123, 567]
print(f'\033[32mOs múltiplos de "3" são:')
for c in meus_numeros:
if c % 3 == 0:
print(c, end='')
print(', ' if c < meus_numeros[-1] else '.', end='')
In this case, the algorithm scans the original list and, if the element is multiple of 3
, displays on the screen.
The
print
is identando at the same level ofif
, just fix the identation that works: https://ideone.com/T1wuoe - in addition, I am voting to close as "typo", following what was defined here: https://pt.meta.stackoverflow.com/q/7476/112052– hkotsubo