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
.
Detail: do
float(67.83643)
is redundant, just use the number67.8343
directly. The same goes fortotal
– hkotsubo