From what I understand, you want to implement a script that contains a function that is able to count the number pares
and ímpares
from a given list.
Due to the fact that there are some errors in your code, I decided to elaborate the following code:
def pares_impares(lis):
cont_par = cont_impar = 0
for item in lis:
if item % 2 == 0:
cont_par += 1
else:
cont_impar += 1
return cont_par, cont_impar
valores = list(map(int, input('Digite todos os valores: ').split()))
cont_pares, cont_impares = pares_impares(valores)
print(f'\033[32mA quantidade de números pares é: {cont_pares}')
print(f'A quantidade de números ímpares é: {cont_impares}')
Note that when we execute this code we receive the following message: Digite todos os valores:
. Right now we must type all the values, in the same line, separated for a single space and press enter
.
After pressing enter
a list of all values will be mounted and sent to the function pares_impares
. Once there, the respective list is traversed by the loop of repetition for
, at the same time that each value of the respective interaction is checked whether it is par
or impar
. If the value is par
, its quantity is accumulated in the variable cont_par
and, where such value is ímpar
, its quantity is accumulated in the variable cont_impar
.
At the end of operations the amount of values is displayed pares
and ímpares
.
Example of script execution
Imagine that we wish to insert the following values: 1, 9, 2, 8, 3, 7, 4, 6, 5
. So when we run the script and get the message: Digite todos os valores
, we must type the following...
1 9 2 8 3 7 4 6 5
... and press enter
.
From that moment the script will perform the operations described above and will show us the following result:
A quantidade de números pares é: 4
A quantidade de números ímpares é: 5
Observing
The size of the list is undefined, that is, if you want to create a list with 3 values, just type the three values and press enter. If you want to create a list with 20 values, just enter twenty values and press enter.
I believe that here:
for lista in range(num):
should be:for num in lista:
– anonimo