If you want to calculate the percentage change, first you should know the logic of the calculation.
The formula for calculating the percentage change is:
variação percentual = ((vf / vi) - 1) * 100
- vf is the current value;
- vi is the old value.
Another thing, in calculating the percentage change there may be values with a positive sign - identifying an increase rate - and values with a negative sign - specifying a fall rate. Moreover, if vf = vi there will be no percentage change.
Well, one of the ways to calculate the percentage change is:
def variacao_percentual(vi, vf):
return ((vf / vi) - 1) * 100
v_inicial = float(input('Insira o valor inicial: '))
v_final = float(input('Insira o valor final: '))
percentual = variacao_percentual(v_inicial, v_final)
if percentual < 0:
print(f'Taxa de queda foi: {percentual:.1f} %')
elif percentual == 0:
print(f'Não existi variação percentual!')
else:
print(f'Taxa de aumento foi: {percentual:.1f} %')
Note that when we execute the code we must enter the initial and final value and then press enter. Later the values will be passed to the function variacao_percentual(vi, vf)
and consequently the value will be calculated and then displayed the result.
Testing the code
Rate of fall:
Imagine we want to discover the percentage change of a product that last week cost R $ 100,00 and today is costing R $ 80,00. In this case, we execute the code and when we receive the message Insira o valor inicial:
, we should type 100 after a enter
and, when we receive the message insira o valor final:
, we should type 80 and press enter.
At this time the calculation will be performed and we will receive as output:
Taxa de queda foi: -20.0 %
Rate of increase:
Imagine we want to discover the percentage change of a product that last week cost R $ 100,00 and today is costing R $ 120,00. In this case, we execute the code and when we receive the message Insira o valor inicial:
, we should type 100 after a enter
and, when we receive the message insira o valor final:
, we should type 120 and press enter.
At this time the calculation will be performed and we will receive as output:
Taxa de aumento foi: 20.0 %
Hello comrade, I edited the post with the doubt. I was able to do what you suggested and return the right value, but I didn’t understand why I can’t use c.append(j) instead of c.append(Function(a,b))
– Kioolz
j is a local variable only within the Variacaopercentual function(a,b)
– Bernardo Lopes
And in addition to what @Bernardo Lopes said, you can add anything within a list less functions, classes and such things...
– Carlos Cortez
@Bernardolopes, so the result of the function only exists inside. A local scope, I get it. Is it possible for me to take the function output and store it in a global scope ? In the future, I intend to use this same code to create a function that calculates successive %-variations.
– Kioolz
@Carloscortez, But can I associate the numerical result generated by a function and fill the list only with the results ? I guess that was my question, but I couldn’t express it correctly.
– Kioolz
Yes, declares already out of function near a, b, c that are global variables
– Bernardo Lopes