How to add values from a for structure?

Asked

Viewed 103 times

-2

So, I need to solve a little problem in my code (I’m very newb), I’m just programming because I need it for my TCC, I don’t know much python but I’m trying to turn around.

On line 126 there’s that formula del_Frs_uth....and it returns me various values, because I have several values of Alpha_e which are calculated according to a formula that receives values of a "for structure", where this structure is in question(line 102) depends on a few more "for structures" higher up... How would I add up all the values of del_Frs_uth???

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

EDIT: I decided to create an example to simplify the question, below

K = 5
N = 2
L = 5
del_a = 1.5
del_b = 0.5
del_c = 0.01

for i in range(1, K + 1, 1):
    a = i*del_a

    for k in range(1, N + 1, 1):
        b = a + (k-1)*del_b

        for j in range(1, L + 1, 1):
            c = j*del_c
            d = b - c
            print(d)

        #COMO SOMAR OS VALORES DE "d"???

Is there any function that adds values of an array? how would I store the values of "d" in an array?? Thank you all

  • 2

    Do not put code images, instead add code as text by formatting through Ctrl + k or on the button {} question editor. In addition try to create a minimum verifiable example of the problem you have, so it’s easier to get an answer.

  • Good morning Isac, I tried to create a simpler example! thanks

1 answer

0


I don’t understand if you want the sum of all d or if you want an array with all the d, the res_soma will contain the sum and res_array will contain all elements

K = 5
N = 2
L = 5
del_a = 1.5
del_b = 0.5
del_c = 0.01
res_soma = 0
res_array = []

for i in range(1, K + 1, 1):
    a = i*del_a

    for k in range(1, N + 1, 1):
        b = a + (k-1)*del_b

        for j in range(1, L + 1, 1):
            c = j*del_c
            d = b - c
            res_soma += d
            res_array.insert(len(res_array), d)

print(res_array) #[1.49, 1.48, 1.47, 1.46, 1.45, ...
print(res_soma) #235.99999999999997
  • Pow, this is exactly what I needed! very obligated. What would be the interpretation of this += d? For each iteration goes adding d?

  • Exactly, res_soma += d is the same as res_soma = res_soma + d

  • thanks a lot, thanks a lot for the help! hug.

Browser other questions tagged

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