为啥还会打印索引后面的数子
打印列表里面的偶数,且索引大于‘’237‘’的就不打印,为啥执行下来最后还打印了566这个数子呢?就大神解答一下。numbers = [386, 462, 47, 418, 907, 344, 236, 375, 823, 566, 597, 978, 328,
615, 953, 345,399, 162, 758, 219, 918, 237, 412, 566, 826, 248,
866, 950, 626, 949, 687, 217,815, 67, 104, 58, 512, 24, 892,
894, 767, 553, 81, 379, 843, 831, 445, 742, 717,958,743, 527]
b = numbers.index(237)
for i in numbers:
if numbers.index(i) < b and i % 2 == 0:
print(i) 应该是大于 237 的值就不打印吧,566 在列表中的索引不大于 237 索引大于237,不是数字的值 因为你有两个566,第一个numbers.index(566)等于9,你遍历到第二个566时,numbers.index(566)还是等于9,所以符合你的条件,会打印上去。如果只是需要在237之前的数字参加检测可以这么写:
numbers = [386, 462, 47, 418, 907, 344, 236, 375, 823, 566, 597, 978, 328,
615, 953, 345,399, 162, 758, 219, 918, 237, 412, 566, 826, 248,
866, 950, 626, 949, 687, 217,815, 67, 104, 58, 512, 24, 892,
894, 767, 553, 81, 379, 843, 831, 445, 742, 717,958,743, 527]
b = numbers.index(237)
for i in range(b):
if numbers % 2 == 0:
print(numbers)
页:
[1]