Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 计算返回的值不正确_Arrays_For Loop_Python 3.x - Fatal编程技术网

Arrays 计算返回的值不正确

Arrays 计算返回的值不正确,arrays,for-loop,python-3.x,Arrays,For Loop,Python 3.x,此函数用于对列表偶数索引中的所有数字求和,然后将此和乘以列表的最后一个数字 checkio = [-37,-36,-19,-99,29,20,3,-7,-64,84,36,62,26,-76,55,-24,84,49,-65,41] def checkzi(array): if len(array) != 0: sum_array = 0 for i in array: x = array.index(i)

此函数用于对列表偶数索引中的所有数字求和,然后将此和乘以列表的最后一个数字

checkio = [-37,-36,-19,-99,29,20,3,-7,-64,84,36,62,26,-76,55,-24,84,49,-65,41]




def checkzi(array):
    if len(array) != 0:
        sum_array = 0
        for i in array:
            x = array.index(i)
            if (x % 2 == 0):
                sum_array += int(i)
                print (sum_array)
        print (sum_array)
        answer = (sum_array) * (array[len(array)-1])
        return (answer)
    else:
        return 0


checkzi(checkio)
我得到的“打印”输出是: -37 -56 -27 -24 -88 -52 -26 29 -36 -36

由此我可以理解,最后一个正确添加的数字是55。55岁之后,84岁没有正确添加。 更重要的是,我得到的最终总数是-1476,而它应该是1968年

我找不到任何理由。反正我也看不见

有人知道吗


谢谢

问题在于
array.index()
将返回值的第一个实例。您有两次值
84
,因此由于第一个索引是奇数,因此您永远不会添加它

您确实需要跟踪索引,而不是依赖于值的唯一性。你和我一起做这个

for idx, val in enumerate(array):
现在,第一个值将是索引,第二个值将是值。测试
idx%2==0
,您可以从这里计算出来

更新以下是完整的代码,明确(我希望)这是如何工作的:

checkio = [-37,-36,-19,-99,29,20,3,-7,-64,84,36,62,26,-76,55,-24,84,49,-65,41]

def checkzi(array):
    if len(array) != 0:
        sum_array = 0
        for idx, x in enumerate(array):
            print "testing element", idx, " which has value ", x
            if (idx % 2 == 0):
                sum_array += x
                print "sum is now ", sum_array
            else:
                print "odd element - not summing"
        print (sum_array)
        answer = (sum_array) * (array[len(array)-1])
        return (answer)
    else:
        return 0

checkzi(checkio)
输出:

testing element 0  which has value  -37
sum is now  -37
testing element 1  which has value  -36
odd element - not summing
testing element 2  which has value  -19
sum is now  -56
testing element 3  which has value  -99
odd element - not summing
testing element 4  which has value  29
sum is now  -27
testing element 5  which has value  20
odd element - not summing
testing element 6  which has value  3
sum is now  -24
testing element 7  which has value  -7
odd element - not summing
testing element 8  which has value  -64
sum is now  -88
testing element 9  which has value  84
odd element - not summing
testing element 10  which has value  36
sum is now  -52
testing element 11  which has value  62
odd element - not summing
testing element 12  which has value  26
sum is now  -26
testing element 13  which has value  -76
odd element - not summing
testing element 14  which has value  55
sum is now  29
testing element 15  which has value  -24
odd element - not summing
testing element 16  which has value  84
sum is now  113
testing element 17  which has value  49
odd element - not summing
testing element 18  which has value  -65
sum is now  48
testing element 19  which has value  41
odd element - not summing
48
很明显,您希望将
print
语句取出-我添加它们是为了帮助解释程序流程。

数组。index()
将始终返回找到值的第一个索引。因此,您要循环遍历每个元素,然后查看它的索引是什么——但是如果有重复的元素(存在),那么您只会看到第一个元素的索引,这导致您在遇到该数字时总是添加(或总是排除)该数字

一种更简洁(更快)的方法是,首先使用Python的以下命令只迭代列表中的偶数元素:


使用内置的sum函数,您甚至可以将整件事情都简化为一行:

def checkzi(array):
    return sum(array[::2]) * array[-1]

你能修一下压痕吗?当前显示的
else:
位置不正确。缩进显然对Python至关重要,因此我不想假设我知道您是如何缩进代码的。是的,对不起,我修复了它否,我不认为“for I in array”返回索引,这就是为什么我在那之后添加了一条语句:x=array.index(I)更重要的是,是的,我在乘以最后一个元素,请阅读这个问题。感谢Floris的回复,但是如果我有一个for循环,它会在第二个84中重复吗?如何解决此问题?请参阅更新的答案以了解解决方案。它会遍历这些值,但无法使用您的方法找到正确的索引。我建议的方法是在不需要查找的情况下同时提供索引和值。很抱歉,我不理解这句话:“对于idx,枚举(数组)中的val”。什么是idx?我不懂这个函数,所以我想不出来。。。你能再解释一下吗?这没什么用。。在我添加这个之后,我得到了一个完全错误的计数:对于idx,枚举(数组)中的val:if(idx%2==0):sum_数组+=int(idx)print(sum_数组),这是一个很好的紧凑解决方案!
def checkzi(array):
    return sum(array[::2]) * array[-1]