Request whole numbers and count even and odd numbers

Asked

Viewed 1,757 times

-4

Question:

"Make a program that asks the user 5 integer numbers and, to final, enter the amount of odd numbers and pairs read."

I’m having a hard time on this issue, I really don’t know how to get it up until then:

for n1 in range(5):
    n1 = input("Digite um numero:")

But I don’t think that’s how you start it. (Remembering that I use Python in Pycharm)

  • 3

    What you’ve already done?

  • In that case, which parts do you you already know do?

  • 1

    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]

  • 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()))))

  • 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?

2 answers

0

From what I understand, you need to implement a program that receives 5 numbers inteiros and then, exiba the amount of values pares and ímpares that were typed. Well, the correct algorithm is just below.

# Iniciando as variáveis contadoras:
numeros_pares = numeros_impares = 0
for c in range(1, 6):
    # Capturando e tratando os valores digitados:
    while True:
        try:
            n = int(input(f'Digite o {c}º número: '))
            break
        except:
            print('\033[31mValor INVÁLIDO! Digite apenas números inteiros!\033[m')

    # Verificando se cada número digitado é par ou ímpar:
    if n % 2 == 0:
        # Contando os números pares:
        numeros_pares += 1
    else:
        # Contando os números ímpares:
        numeros_impares += 1

# Imprimindo a quantidade de números pares e ímpares:
print(f'\033[32mA quantidade de números pares é: {numeros_pares}')
print(f'A quantidade de números ímpares é: {numeros_impares}\033[m')

See here the functioning of the algorithm.

Note that this program has a tratamento of errors and exceptions. Because, it only allows numbers inteiros.

Then it counts the numbers pares and ímpares to subsequently display their quantities.

0

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)

Browser other questions tagged

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