You were on the right track.
Let’s start by creating two variables to count how many even and odd numbers are reported.
contador_pares = 0
contador_impares = 0
Then we loop asking for the numbers pro user, following the approach you had started on your attempt.
for i in range(5):
entrada = input('Digite um numero: ')
numero = int(entrada)
if numero % 2 == 0:
contador_pares += 1
else:
contador_impares += 1
Note that inside the loop you need to convert the input to int
, because the return of function input
It’s a string, and we need a number. Also, to test if a number is even, we make its module by 2. If the module is 0 it means that it is divisible by 2 and therefore is even.
After the loop, simply display the results.
print('Quantidade pares:', contador_pares)
print('Quantidade impares:', contador_impares)
Follow the full code.
contador_pares = 0
contador_impares = 0
for i in range(5):
entrada = input('Digite um numero: ')
numero = int(entrada)
if numero % 2 == 0:
contador_pares += 1
else:
contador_impares += 1
print('Quantidade pares:', contador_pares)
print('Quantidade impares:', contador_impares)
What you’ve already done?
– Breno
In that case, which parts do you you already know do?
– bfavaretto
Hello @Mel, Welcome to Sopt, before you start a look at our [Tour] =D, now about your question, it will be more welcomed by the community if you add more information like: I’m having second thoughts on that part ..., I’ve been able to do that so far .... Your question is currently asking someone to do the exercise for you. [Ask]
– Icaro Martins
print('Contagem de pares e ímpares, respectivamente:', *map(sum, zip(*((int(n) % 2 == 0, int(n) % 2 != 0) for n in input('Informe 5 números separados por espaço: ').split()))))
– Gabriel
There’s a delete link in the question if you want. But I edited the content and I’m reopening, who knows now you can’t get an answer?
– bfavaretto