在Python函数中获取“无”而不是零值

问题描述 投票:1回答:1

我想制作一个程序,在内存中放置一个数组(1x9的数字从0到9),我想检查我创建的数组是否以前使用过。我将使用2个功能。

addMemory(list,previousStates)将新创建的数组添加到内存中,checkMemory(list,previousStates)检查数组以前是否使用过。如果使用数组,则返回1,否则返回0。

我通过假设数组的每个元素都是9位数的数字,将数组转换为数字。

例如:[2,5,3,4,1,6,8,9,7]存储为253.416.897。我想测试我的功能。第一次打印具有空内存,并检查内存并添加阵列并检查新内存。

输出应该是

0

1

但我明白了

没有

1

为什么我得到'无'而不是0?你能帮帮忙吗?

def addMemory(newlist,previousStates):
    count = 0
    for i in range(0,8):
        count += count + newlist[i] * 10**(8-i)
    previousStates.append(count)
    return previousStates

def checkMemory(newlist,previousStates):
    count = 0
    for i in range(0,8):
        count += count + newlist[i] * 10**(8-i)
    for i in range(len(previousStates)):
        if(previousStates[i] == count):
            return 1
        return 0

def main():
    a = [5,3,4,7,8,9,1,2,6]
    previousStates = []
    print(checkMemory(a,previousStates))
    addMemory(a,previousStates)
    print(checkMemory(a,previousStates))

main()
python arrays
1个回答
4
投票

在你的checkMemory函数中,因为len(previousStates)起初为零,你的for语句永远不会被执行,因此你的函数永远不会到达任何return

© www.soinside.com 2019 - 2024. All rights reserved.