Jul-24-2018, 06:37 AM
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
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
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs