简体   繁体   English

为什么我的代码不会对列表中负数的值求和?

[英]Why won't my code sum the value of the negative numbers in the list?

given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]
total7 = 0
i = 0
while i <= len(given_list3) and given_list3[i] <= 0:
    total7 += givenlist3[i]
    i += 1
print(total7)

The code is producing a result of 0, and I want to to result in: -2 + -3 + -5 + -7 = -17代码产生的结果为 0,我想得到:-2 + -3 + -5 + -7 = -17

You can use comprehension:您可以使用理解:

given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]
output = sum(x for x in given_list3 if x < 0)
print(output) # -17

In your current code, you are exiting the while loop even before the first iteration, because the second condition given_list3[i] <= 0 is false (since the first item 7 is greater than 0 ).在您当前的代码中,您甚至在第一次迭代之前就退出了while循环,因为第二个条件given_list3[i] <= 0为假(因为第一项7大于0 )。 If you want a working version, try the following.如果您想要一个工作版本,请尝试以下操作。 (Also you need to use i < len(...) , not i <= len(...) .) (此外,您需要使用i < len(...)而不是i <= len(...) 。)

given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]
total7 = 0
i = 0
while i < len(given_list3):
    if given_list3[i] <= 0:
        total7 += given_list3[i]
    i += 1
print(total7) # -17

A while loop stops when the condition is false.当条件为假时, while循环停止。 So your loop stops when it gets to the first positive value.所以当它达到第一个正值时,你的循环就会停止。 Since the first element of the list is positive, the loop doesn't do anything.由于列表的第一个元素是正数,循环不做任何事情。

The negative condition should be if inside the loop.否定条件应该是if在循环内。

Also, list indexes go to len(list)-1 , so the length condition should be < , not <= .此外,列表索引转到len(list)-1 ,因此长度条件应该是< ,而不是<=

while i < len(given_list3):
    if given_list3[i] <= 0:
        total7 += given_list3[i]
    i += 1

You should get out of the habit of looping over list indexes.你应该改掉循环列表索引的习惯。 Use for-in :使用for-in

for item in given_list3:
    if item <= 0:
        total7 += item

You never iterate through your while loop.你永远不会遍历你的 while 循环。 This is the first iteration of your while loop when i = 0:当 i = 0 时,这是 while 循环的第一次迭代:

while 0 <= len(given_list3) and given_list3[0] <= 0:而 0 <= len(given_list3) 和 given_list3[0] <= 0:

0 <= len(given_list3) - true; 0 <= len(given_list3) - 真; 0 is less than 10 given_list3[0] <= 0 - false; 0 小于 10 given_list3[0] <= 0 - false; given_list3[0] = 7 and 7 is not less than or equal to 0. given_list3[0] = 7 且 7 不小于或等于 0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么这个清单不合计? - Why won't this list sum? 我想找到数字中数字的平方和,我的代码适用于正数但似乎不适用于负数 - I want to find the sum of squares of digits in a number, my code work fine for positive numbers but doesn't seem to work with negative numbers 只求列表中负数的总和 - to find the sum of only the negative numbers of the list 为什么我的运动代码不起作用? - Why won't my Movement code work? 返回正数计数和负数总和的列表的函数 - Function that returns a list of the count of positive numbers and sum of negative numbers 当我在数据库而不是简单列表上尝试时,为什么我的代码不能在 Visual Studio 上运行? - Why won't my code run on Visual Studio when I try it on a database instead of a simple list ? 为什么我的代码不能根据列表元素正确地确定它们是否是质数? - Why my code doesn't deciding correctly from list elements that they are prime numbers or not? 为什么visual studio代码不能运行我的代码? - why won't visual studio code run my code? 在列表中将值1分配给正数,将0分配给负数 - Assign value of 1 to positive and 0 to negative numbers in a list 为什么我的列表不会填充? IndexError:列出超出范围 - Why won't my list populate? IndexError: List Out of Range
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM