0

i wrote this code in python:

list=[3,5,7,6,-9,5,4]
i=0
total=0
while i<len(list) and list[i] > 0:
     total+=list[i]
     i+=1
print(total)

and instead of getting the total of all positive numbers i only get the total of numbers that are located before the negative one, i'm not sure what i'm doing wrong, this is just my fourth code in python i'd like some help^^

joxavy
  • 9
  • 8
  • 1
    Here you should not be using a `while` loop. Think about it: a while loop will continue executing *while numbers remain positive* and stop as soon as it encounters the first negative number. Use `for` loop instead or change conditional expressions in your while loop. – NotAName Sep 20 '20 at 23:52
  • @pavel it is not an issue that ```while``` loop is being used or ```for``` loop. The code will work well in both the cases, considered that appropriate conditions are put. – Swati Srivastava Sep 21 '20 at 00:06
  • @SwatiSrivastava While a `while` will work, it is the wrong tool to use in this case. Iteration over containers is more simple with a `for` loop. No need for initializing or incrementing an index, and if you need the index, use `for i,item in enumerate(lst)`. Even more simple for this case is a list comprehension. – Mark Tolonen Sep 21 '20 at 00:10
  • @MarkTolonen I agree with ```for``` works better in iterating conditions, but using ```while``` may not be wrong, just inefficient. – Swati Srivastava Sep 21 '20 at 00:17

3 Answers3

1

That is because you have the condition that the loop ends if you encounter a negative number. You can first remove that condition from the loop.

while i<len(list):

Next, you only want to add non-negative numbers so, use a conditional statement in the loop with the condition that you want.

if list[i] > 0:
Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192
  • thank you! i've tried this solution but now i only get in the output the first number which is 3 – joxavy Sep 20 '20 at 23:59
0

Try to understand the working of while loop. It works as long as i < len(list and list[i] > 0. When it reaches the value -9, the while loop's second condition gets false and it terminates. Hence no sum is calculated after first negative integer is encountered.

To solve this, do

lis = [3, 5, 7, 6, -9, 5, 4]
sum = 0
for i in lis:
    if i > 0:
        sum += i

For your while loop code, use

lis = [3, 5, 7, 6, -9, 5, 4]
i = 0
sum = 0
while i < len(lis):
    if lis[i] > 0:
        sum += lis[i]
    i = i + 1

Also, although you can use a variable name as list, it is advised not to do so in Python as list is also a keyword.

Swati Srivastava
  • 1,102
  • 1
  • 12
  • 18
0

Do not use a while loop in this case. Iteration over items in a container is easier in Python with a for loop. Plus the logic is wrong and exits the while loop early when it fails the and list[i] > 0 condition.

lst = [3,5,7,6,-9,5,4]
total = 0
for item in lst:
    if item > 0:
        total += item
print(total)

Also, don't use list as a variable name. That replaces the built-in list type and is called "shadowing".

You can also use a list comprehension and simplify the code even more:

lst = [3,5,7,6,-9,5,4]
total = sum([item for item in lst if item > 0])
print(total)
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251