I’m trying to write a program that calculates the monthly percentage of a distributor

Asked

Viewed 431 times

0

I tried to solve it in Python but it did not happen I am already so many hours in that I can not think anymore so I decided to raise my hand. This is the exercise:

Given the monthly billing value of a distributor, detailed below per state:

SP - R$67,836.43

RJ - R$36,678.66

MG - R$29,229.88

ES - R$27,165,48

Other - R$19,849.53

Write a program in the language you want where you calculate the percentage of representation that each state had within the total monthly billing value of the distributor.

What I wrote in Python:

sp = float(67.83643)
rj = float(36.67866)
mg = float(29.22988)
es = float(27.16548)
out = float(19.84953)
total = float(sp + rj + mg + es + out)
print(total)
psp = ((sp*total)/100)
prj = ((rj*total)/100)
pmg = ((mg*total)/100)
pes = ((es*total)/100)
pout = ((out*total)/100)

print('Porcentagem de SP {}'.format(psp))
print('Porcentagem de RJ {}'.format(prj))
print('Porcentagem de MG {}'.format(pmg))
print('Porcentagem de ES {}'.format(pes))
print('Porcentagem de OUT {}'.format(pout))
  • Detail: do float(67.83643) is redundant, just use the number 67.8343 directly. The same goes for total

2 answers

2

Your problem is in the percentage formula. You must divide the value of a state by the total and not multiply it. Then multiply by 100% to find the percentage (you reversed the operators):

sp = float(67.83643)
rj = float(36.67866)
mg = float(29.22988)
es = float(27.16548)
out = float(19.84953)
total = float(sp + rj + mg + es + out)
print(total)
psp = ((sp/total)*100)
prj = ((rj/total)*100)
pmg = ((mg/total)*100)
pes = ((es/total)*100)
pout = ((out/total)*100)

print('Porcentagem de SP {}'.format(psp))
print('Porcentagem de RJ {}'.format(prj))
print('Porcentagem de MG {}'.format(pmg))
print('Porcentagem de ES {}'.format(pes))
print('Porcentagem de OUT {}'.format(pout))

0

Imagine you have to calculate monthly these values. In this case, you have to implement the function input() to receive these values each month that needs.

Another thing the calculation of percentagem that you wish to calculate in this program is a percentagem relativa. In this situation you need to calculate the percentage of cada estado depending on the faturamento total.

Upon these observation I implemented the following algorithm...

# Definindo os estados para uma melhor manipulação:
e = ['SP', 'RJ', 'MG', 'ES', 'OUTROS']

faturamento = list()
for c in e:
    # Capturando e tratando os valores digitados:
    while True:
        try:
            v = float(input(f'Digite o faturamento mensal de {c}: '))
            if v < 0:
                print('\033[31mValor INVÁLIDO! Digite apenas valores maiores ou iguais a "0":\033[m')
            break
        except:
            print('\033[31mValor INVÁLIDO! Digite apenas valores reais!\033[m')

    # Armazenando os valores digitados na lista faturamento
    faturamento.append(v)

# Calculando o faturamento total da distribuidora:
faturamento_total = sum(faturamento)
print(f'\033[32mO faturamento total da Distribuidora foi: R$ {faturamento_total:.2f}'.replace('.', ','))

# Calculando e exibindo o percentual relativo de cada filial de cada estado:
cont = 0
for i in faturamento:
    cont += 1
    percentual = ((i / faturamento_total) * 100)
    print(f'O percentual de faturamento de {e[cont - 1]} é: {percentual:.2f} %')

See here the operation of the programme.

Note that the first thing I implemented in this program was a lista. For the acronyms of estados, except the last one that is outros.

Then I created a block of codes where they will be capturados and tratados each input value.

Note that for this algorithm to work correctly the typed values will have to be as follows xx.xxxxx, that is, for you to enter the value R$ 67.836,43, you will have to type...

67.83643

If each entered value is correct, it will be inserted in the list faturamento.

Then the program calculates the faturamento total of the distributor. This total revenue is the sum of all the invoices obtained by each subsidiary.

Subsequently the relative percentage of each filia depending on the faturamento total.

Browser other questions tagged

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