我正在尝试在 python 中编写代码来过滤前 n 个整数的列表,这些整数在除以时余数为 1。但是,它看起来并没有根据我拥有的打印语句检查列表中的每个整数。我不知道为什么。我刚开始学习代码,所以请耐心等待。我怀疑这是我没有看到的明显东西。
以下是我到目前为止的代码:
def main():
checkUpTo = 20
listOfInts = list(range(checkUpTo))
filterList = [3]
boolList = []
for d in filterList:
print("checking list: " + str(listOfInts) + " for rem=1 when divided by " + str(d))
for x in listOfInts:
print("checking: " + str(x))
isDivisible = checkIfDivisible(x,d)
if isDivisible==False:
listOfInts.remove(x)
print("got list: " + str(listOfInts))
print(listOfInts)
def checkIfDivisible(number,divisor):
remainder = number % divisor
print(str(number) + "%" + str(divisor) + "=" + str(remainder))
if number % divisor == 1:
return True
if number % divisor != 1:
return False
if __name__ == "__main__":
main()
谢谢您的帮助
编辑:我的输出:
checking list: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] for rem=1 when divided by 3
checking: 0
0%3=0
checking: 2
2%3=2
checking: 4
4%3=1
checking: 5
5%3=2
checking: 7
7%3=1
checking: 8
8%3=2
checking: 10
10%3=1
checking: 11
11%3=2
checking: 13
13%3=1
checking: 14
14%3=2
checking: 16
16%3=1
checking: 17
17%3=2
checking: 19
19%3=1
got list: [1, 3, 4, 6, 7, 9, 10, 12, 13, 15, 16, 18, 19]
[1, 3, 4, 6, 7, 9, 10, 12, 13, 15, 16, 18, 19]
不知道为什么它不检查 1,3 等。