When I use the following code I get a correct answer of 28.
#want to find sum of only the positive numbers in the list
#numbers = [1,3,9,10,-1,-2,-9,-5]
numbers = [4,6,2,7,9]
numbers.sort(reverse = True) # sets to greatest to smallest numbers
total = 0
_index = 0
#while numbers[_index] > 0 and _index < len(numbers):
while _index < len(numbers) and numbers[_index] > 0:
total += numbers[_index]
_index += 1
print(total)
When I try the following code I get "IndexError: list index out of range", I only changed the while loop. Why does one condition have to be first, does it not check each one before running this loop?
#want to find sum of only the positive numbers in the list
#numbers = [1,3,9,10,-1,-2,-9,-5]
numbers = [4,6,2,7,9]
numbers.sort(reverse = True) # sets to greatest to smallest numbers
total = 0
_index = 0
while numbers[_index] > 0 and _index < len(numbers):
#while _index < len(numbers) and numbers[_index] > 0:
total += numbers[_index]
_index += 1
print(total)