+ 1

I want the result to be 6(or < 9), because that is the condition ( srry google traslate) im a beginner

X = 0 Y = [ ] Z = [1, 2, 3, 4, 5] If X < 9: .... For i in Z: ......... X = X + i ......... Y.append(X) ...........print (Y) Result: [1] [1, 3] ....... [1, 3, 6, 10, 15]

25th Dec 2016, 6:22 PM
Julio Csar
Julio Csar - avatar
2 Answers
+ 2
As far as I understood, what you want to achieve is sum Z's items, as long as they're under 9, and add tthem into a new array Y. If that is what you want to do: X = 0 Y = [ ] Z = [1, 2, 3, 4, 5] for i in Z: X = X + i if X < 9: Y.append(X) print(Y) else: break First do your for loop. In the loop add i to X. In the first loop: (X = X + i) equals (X = 0 + 1). 1 < 9, so you add it into the array "Y". 2) Loop: X = 1 + 2, 3 < 9 so Y.append(X) 3) Loop: X = 3 + 3, 6 < 9 -//- 4th Loop: X = 6 + 4, 10 < 9 is false, so nothing is added Result: [1] [1,3] [1,3,6] P.S: I added an else statement if x is not smaller than 9, so the loop finishes (break). If your array larger it's better to break it and not let it run for all items in Z. I hope this is what you asked for!! If you only want to output 6, use this: X = 0 Z = [1, 2, 3, 4, 5] for i in Z: X = X + i if not X < 9: result = X - i print(result) break You add i to X for every item in Z. If x is not smaller than 9 anymore (in this case after 4 is added to X) i is substracted from X, because it was added before in X = X + 1, but made X larger than 9. Then we print the result (6) and break the loop. :)
25th Dec 2016, 10:23 PM
Madison
Madison - avatar
0
Muah thanks
26th Dec 2016, 12:34 AM
Julio Csar
Julio Csar - avatar