我想编写一个程序,将一个数组(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
但是我明白了
None
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()
最佳答案
在你的 checkMemory
函数中,因为 len(previousStates)
一开始是零,你的 for
语句永远不会被执行,因此你的函数永远不会到达任何返回
关于python - 在 Python 函数中获取 "None"而不是零值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55201640/