Python Forum
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
while loop question
#1
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

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
Reply
#2
Please use code tags. I added them for you this time, but check the BBCode link in my signature below to learn how to do it yourself.

You should just do a for loop over the items in the list, and only add them to the total if the item is less than zero.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
To clarify why the while loops are failing: You get out of the first while loop when the sign changes. You have no way to get back to it when the sign changes back.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
Thanks for the clarification and editing.
Reply
#5
just to mention that using while in this case/this way is how to say ... unusual

if you have to use while loop, although not recommended
my_list = [-3, 4, 6, 7, -10, -3, 8, -4]
total = 0
j = 0
while j < len(my_list):
    if my_list[j] < 0:
        total += my_list[j]
    j += 1
print(total)
two more pythonic ways, there are also others:
my_list = [-3, 4, 6, 7, -10, -3, 8, -4]
total = 0
for number in my_list:
    if number < 0:
        total += number
print(total)
my_list = [-3, 4, 6, 7, -10, -3, 8, -4]
print(sum(number for number in my_list if number < 0))
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Many iterations for loop question adesimone 9 1,815 Nov-12-2022, 07:08 PM
Last Post: deanhystad
  Please check whether the code about the for loop question is correct. (SyntaxError) lilliancsk01 10 2,550 Nov-08-2022, 01:25 PM
Last Post: deanhystad
  Beginner Python Question: FIzz Buzz using while loop camoyn13 2 1,774 Sep-20-2022, 09:00 AM
Last Post: deanhystad
  Repeat question (for loop) rs74 7 6,438 Jun-17-2020, 03:17 PM
Last Post: rs74
  Question about running comparisons through loop from input value Sunioj 2 2,390 Oct-15-2019, 03:15 PM
Last Post: jefsummers
  Loop question kraven 3 3,617 Sep-10-2017, 07:31 AM
Last Post: wavic
  Question about loop Pires 4 3,542 Jul-23-2017, 03:01 AM
Last Post: Pires
  Udacity while loop question liquidmetalrob 6 5,328 Jul-21-2017, 02:56 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020