Problem when calculating percentage (result is always "0")

Asked

Viewed 11,769 times

2

I finished a program in Python (perfected with a response obtained right here in Stack Overflow), but I’m trying to include a percentage calculation that isn’t working. Although everything else works, the percentage result is always "0".

Is there a problem in the code below? The file . csv contains names of Higher Education courses and numbers of vacancies offered, registered candidates and tickets in each.

import csv

curso_desejado = input('Qual o curso? ')
vagas = 0
inscritos = 0
ingressos = 0
arquivo = open('censo2016.csv', encoding='utf8')
for registro in csv.reader(arquivo):
    if registro[0] == curso_desejado:
        vagas += int(registro[1])
        inscritos += int(registro[2])
        ingressos += int(registro[3])
        porcentagem = int(ingressos / vagas) * 100
print(f'O número de vagas oferecidas em {curso_desejado} é: {vagas}')
print(f'O número de inscritos em {curso_desejado} é: {inscritos}')
print(f'O número de ingressantes em {curso_desejado} é: {ingressos}')
print(f'O percentual de vagas preenchidas em {curso_desejado} é: {porcentagem}'"%")

The result for the "Right" input, for example, appears like this:

Qual o curso? Direito
O número de vagas oferecidas em Direito é: 245956
O número de inscritos em Direito é: 1204636
O número de ingressantes em Direito é: 206623
O percentual de vagas preenchidas em Direito é: 0%

2 answers

2

Your error in the original program is on this line:

porcentagem = int(ingressos / vagas) * 100

Well, tickets / vacancies, will always give a number between 0 and 1. This transformed into whole will always catch only the whole part, which is 0. Then you multiply 0 by 100.

In the version that works, you have removed the parentheses, and the call to int - and the account was right. But if you wanted an entire value, just do:

porcentagem = round((ingressos / vagas) * 100)

Okay, now we have a number between 0 and 100 - the number between 0 and 1 is multiplied by 100, and then rounded. Use round there is better because it will not truncate the decimal part: 0.7% will turn 1% and not 0%.

(the internal parentheses pair is only to increase readability, and you do not have to worry, when reading the expression, whether first is made the division or first the account "vacancies * 100", which would give different results. The rules of the language are clear, but they are small adjustments for readability that make a program more maintainable)

1

Oops, now it worked. Apparently percentage should not be presented as integer (that would be the explanation?).

It worked that way:

import csv

curso_desejado = input('Qual o curso? ')
vagas = 0
inscritos = 0
ingressos = 0
arquivo = open('censo2016.csv', encoding='utf8')
for registro in csv.reader(arquivo):
    if registro[0] == curso_desejado:
        vagas += int(registro[1])
        inscritos += int(registro[2])
        ingressos += int(registro[3])
        porcentagem = ingressos / vagas * 100
print(f'O número de vagas oferecidas em {curso_desejado} é: {vagas}')
print(f'O número de inscritos em {curso_desejado} é: {inscritos}')
print(f'O número de ingressantes em {curso_desejado} é: {ingressos}')
print(f'O percentual de vagas preenchidas em {curso_desejado} é: {porcentagem:.1f}'"%")

Browser other questions tagged

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