Jul-24-2018, 05:44 AM
(This post was last modified: Jul-24-2018, 05:48 AM by ichabod801.)
Hello everyone,
I am a beginner of Python and I have some questions regarding how to use while loop. I would like to calculate the sum of all the negative values from the lists below.
For list b, the codes stops after the first item; for list a, the list stops after item 6. My codes are below. It works only when the negative numbers are consecutive. Not sure what needs to change in order to achieve my goals.
Thank you
Best,
Andy
the result is -3
-----------------------------
the result is -13
I am a beginner of Python and I have some questions regarding how to use while loop. I would like to calculate the sum of all the negative values from the lists below.
For list b, the codes stops after the first item; for list a, the list stops after item 6. My codes are below. It works only when the negative numbers are consecutive. Not sure what needs to change in order to achieve my goals.
Thank you
Best,
Andy
b = [-3, 4, 6, 7, -10, -3, 8, -4] total2 = 0 j = 0 while b[j] > 0: j += 1 while b[j] < 0: total2 += b[j] j += 1 print(total2)///
the result is -3
-----------------------------
a = [3, 4, 6, 7, -10, -3, 8, -4] total3 = 0 j = 0 while a[j] > 0: j += 1 while a[j] < 0: total3 += [j] j += 1 print(total3)///
the result is -13