Join three similar codes into one

Asked

Viewed 344 times

2

I am working with a table that shows the number of vacancies offered, candidates enrolled and enrolled in higher education courses. I want to make a program that allows the visualization of this data from the input user (say he wants to know how many vacancies were offered and how many people entered in law courses... he type "Right" and the program shows these numbers).

I managed to make the codes work, but only one by one, in three different programs. Is it possible to unite these three in one code?

1)

import csv

curso_desejado = input('Qual o curso? ')
vagas = 0
arquivo = open('oks4c.csv', encoding='utf8')
for registro in csv.reader(arquivo):
    vagas_oferecidas = registro[1]
    curso = registro[0]
    if curso == curso_desejado:
        vagas += int(vagas_oferecidas)
print(f'O número de vagas oferecidas em {curso_desejado} é: {vagas}')

2)

import csv

curso_desejado = input('Qual o curso? ')
inscritos = 0
arquivo = open('oks4c.csv', encoding='utf8')
for registro in csv.reader(arquivo):
    candidatos_inscritos = registro[2]
    curso = registro[0]
    if curso == curso_desejado:
        inscritos += int(candidatos_inscritos)
print(f'O número de inscritos em {curso_desejado} é: {inscritos}')

3)

import csv

curso_desejado = input('Qual o curso? ')
ingressos = 0
arquivo = open('oks4c.csv', encoding='utf8')
for registro in csv.reader(arquivo):
    ingressantes = registro[3]
    curso = registro[0]
    if curso == curso_desejado:
        ingressos += int(ingressantes)
print(f'O número de ingressantes em {curso_desejado} é: {ingressos}')

1 answer

4


Would be basically this:

import csv

curso_desejado = input('Qual o curso? ')
vagas = 0
inscritos = 0
ingressos = 0
arquivo = open('oks4c.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])
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}')

I put in the Github for future reference.

I mean, I took the part that doesn’t change and I added the parts that change.

It has to decrease 6 lines (although it will end up needing more 3 or even more), but I think it does not compensate.

  • Was open ingressantes =

  • Yes, thank you, corrected.

  • Perfect! It worked. Taking advantage, would it be feasible to do an operation (to be shown in a fourth print line) showing that the X tickets of such a course represent that Y% of the total vacancies offered was occupied? Or Python percentage is complicated?

  • @William Percent is simple in any language, just know arithmetic. This is another problem.

  • In fact, it’s a simple @Maniero calculation! I thought I’d need to do some sort of more complicated arrangement. But I was left wondering where to put it. In which part of the code I could insert the line porcentagem = ingressos / vagas * 100 or better porcentagem = int(ingressos / vagas) * 100 for what, in addition to the number of vacancies, registered and tickets, downstairs the program also showed the percentage of filled vacancies? I tried to use these commands, but the line print(f'O percentual de vagas preenchidas em {curso_desejado} é: {porcentagem}') does not show the result...

Browser other questions tagged

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