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

1

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

Is there a problem in the code below? The .csv file contains names of higher education courses and numbers of offered positions, registered candidates and tickets in each one.

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 input "Right", 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%
    
asked by anonymous 10.01.2018 / 03:03

2 answers

2

Your original program error is on this line:

porcentagem = int(ingressos / vagas) * 100

Well, tickets / vacancies, will always give a number between 0 and 1. This turned into integer will always get only the whole part, which is 0. Then you multiply 0 percent.

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

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

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

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

    
10.01.2018 / 14:53
0

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

It worked like this:

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}'"%")
    
10.01.2018 / 03:17