How does this function work?

Asked

Viewed 49 times

-3

That role was in my python test in college and I can’t understand it, more specifically the part of for.

def exerc4(a,b,c,d,e,f):
if(a > b):
    x = a + b
elif (e > f):
    x= e + f
else:
    x = a + b + c + d + e + f
y =[a, b, c, d, e, f, x]
u = 0
for x in range(0,7):
    u += y[x] + y[0]
    print(u)

exerc4(20,30,40,50,60,70)

1 answer

1


This function returns the sum of an array of [a,b,c,d,e,f,x] each element being added to the element a, somewhat reminiscent of Fibonacci

exerc4(20,30,40,50,60,70)
a = 20
b = 30
c = 40
d = 40
e = 60
f = 70

def exerc4(a,b,c,d,e,f):
  # 20 > 30: Falso
  if(a > b):
    x = a + b

  # 60 > 70: Falso
  elif (e > f):
    x= e + f
 else:
   # x = 20 + 30 + 40 + 50 +60 + 70 = 270
   x = a + b + c + d + e + f
   y =[a, b, c, d, e, f, x]
   u = 0
   for x in range(0,7):
     u += y[x] + y[0]
   print(u)

Exit

u  = u + y[x] = Y[0]
40 = 0 + 20 + 20 
90 = 40 + 30 + 20
150 = 90 + 40 + 20
220 = 150 + 50 + 20
300 = 220 + 60 + 20
390 = 300 + 70 + 20
680 = 390 + 270 + 20

Exit

40
90
150
220
300
390
680

Browser other questions tagged

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