Separating numbers into even and odd lists - Python

Asked

Viewed 1,578 times

2

My code needs to read multiple numbers and put them in a list, then create two extra lists containing only even and odd values respectively from the previous list.

Example:

Entrada = [3, 7, 8, 11, 16, 20]
Saída = Lista completa: [3, 7, 8, 11, 16, 20]
        Lista de pares: [8, 16, 20]
        Lista de ímpares: [3, 7, 11]

But the values are not being added in the lists of even and odd numbers, what is the problem with my code?

valores = pares = impares = list()

while True:
    valores.append(int(input('Digite um valor: ')))

    continuar = ' '
    while continuar not in 'SN':
        continuar = str(input('Continuar? [S / N] ')).upper()[0]
    if continuar in 'N':
        break

print(f'Lista completa: {valores}')

for num in valores:
    if num % 2 == 0:
        pares.append(num)
    else:
        impares.append(num)

print(f'Lista de pares: {pares}')
print(f'Lista de ímpares: {impares}')

3 answers

3


The problem with your code is in

valores = pares = impares = list()

This generates only one list but with three variables referencing it. Thus, the odd-pair check loop never stops running as elements are always added to each iteration. The solution to this is to define three separate lists:

valores = []
pares = []
impares = []

1

To resolve this issue you must implement an algorithm that monte a list of all values digitados by the user, then separe the values ímpares of pares and subsequently display them on separate lists.

For this you can implement the following algorithm...

# Criando uma lista com todos os valores digitados:
valores = list(map(int, input('Digite todos os valores desejados: ').split()))

# Separando os valores em pares e ímpares:
impares = list()
pares = list()
for c in valores:
    if c % 2 != 0:
        impares.append(c)
    else:
        pares.append(c)

# Exibindo as listas de valores pares e ímpares:
print(f'\033[32mOs números pares são: {pares}')
print(f'Os números ímpares são: {impares}')

See here the operation of the programme.

Note that when you run this program you get the following solicitação...

Digite todos os valores desejados:

Right now you have to type all the values desired in same line, separated by only 1 space, as exemplified below.

4 6 8 9 12 23

After you have entered all the values just type enter. At this time the program will separate the values impares and pares and subsequently display them.

Also note that to close the inserção of values in lista, just press the button enter.

0

It’s like Murgalha said, see if it doesn’t suit you better:

lista_completa = [3, 7, 8, 11, 16, 20]
lista_pares = []
lista_impares = []

for c in lista_completa:
  if c % 2 == 0:
    lista_pares.append(c)
  else:
    lista_impares.append(c)

print(lista_impares)
print(lista_pares)
  • Your reply does not take into account that the algorithm must be implemented in order to receive n values according to the user.

Browser other questions tagged

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