Doubt about storing the function result in a list and the types of stored variables

Asked

Viewed 73 times

0

Home Post

I am making a simple code that gives me the variation % between two numbers, A and B. As soon as he calculates this variation %, he should attach the result of the operation in a list that at first is empty.

But I get the None result, could you tell me why it is being filled with None and not with the float value generated by the function ?

Also, when I press type(Variacaopercentual(a,b)) it does not return me a float.

a = float(input("Insira o valor inicial")
b = float(input("Insira o valor final")
c = []
def VariacaoPercentual(a,b):
print(((a/b)-1)*100)
c.append(VariacaoPercentual(a,b))
print(c)

inserir a descrição da imagem aqui

Edit 1 : ---------------------

inserir a descrição da imagem aqui

'Cause I can’t do c.append(j), where j is the very value that the function returns?

Edit 2 --- Reply in comments.

3 answers

1

The function is not returning anything, has no return, puts the Return

 def VariacaoPercentual(a,b):
   print(((a/b)-1)*100)
   return ((a/b)-1)*100
  • 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))

  • 1

    j is a local variable only within the Variacaopercentual function(a,b)

  • 1

    And in addition to what @Bernardo Lopes said, you can add anything within a list less functions, classes and such things...

  • @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.

  • @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.

  • 1

    Yes, declares already out of function near a, b, c that are global variables

Show 1 more comment

1

a = float(input("Insira o valor inicial")
b = float(input("Insira o valor final")
c = []
def variacao_percentual(a,b):
    print(((a/b)-1)*100)
    c.append(((a/b)-1)*100)
    print(c)
variacao_percentual(a,b)

Edit 1 : to be able to store the values in the list, it is possible to do the following:

c = []
def variacao_percentual():
    a = float(input("Insira o valor inicial"))
    b = float(input("Insira o valor final"))
    print(((a/b)-1)*100)
    c.append(((a/b)-1)*100)
    print(c)

and call the function whenever you need to insert new data in the list

variacao_percentual()

recalling that this list is not a database and the data are temporary.

  • Hi buddy, by the method you described, it displays the C list with only one result. Soon, when I do the process again, the list will be size 1 again but with another value. That’s what happened in my IDLE

  • 1

    So it’s not a simple program, as described in question =P

  • 1

    You need to see the results every time you insert?... could you make a loop with some stop criteria

  • Yes, by the way. I was able to store the list with more values, the problem was that I was not separating the lines of code properly in Vscode and the list restarted every time I compiled the code.

  • Thank you for this method. I will adapt the code to your method and make an Edit with your contribution and that of the Solkarped. I didn’t think the problem wouldn’t be simple, this code came up while I was trying to study a math book.

1


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
  1. vf is the current value;
  2. 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 %

Browser other questions tagged

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