我试图在Windows10上用Python3.6.5编写一个程序,它接受整数,然后打印出奇数。在
这是我的代码:def listInput():
aList = list()
while True:
num = input("Please input a number:")
if num == 'done':
break
num = int(num)
aList.append(num)
return aList
def isOddNumber(y):
if y%2 == 0:
return False
else:
return True
def filterOddInList(a):
i = len(a)
while i >= 0:
item = a[(i-1)]
odd = filterOddInList(item)
if odd == False:
x.pop()
def main():
a = listInput()
print(a)
a = filterOddInList(a)
print(a)
main()
这是应该发生的事情:
^{pr2}$
实际情况如下:Please input a number:1
Please input a number:2
Please input a number:3
Please input a number:4
Please input a number:5
Please input a number:done
[1, 2, 3, 4, 5]
Traceback (most recent call last):
File "C:\Users\Jerry Cui\Documents\New folder\Homework\oddonly.py", line 30, in
main()
File "C:\Users\Jerry Cui\Documents\New folder\Homework\oddonly.py", line 27, in main
a = filterOddInList(a)
File "C:\Users\Jerry Cui\Documents\New folder\Homework\oddonly.py", line 20, in filterOddInList
odd = filterOddInList(item)
File "C:\Users\Jerry Cui\Documents\New folder\Homework\oddonly.py", line 17, in filterOddInList
i = len(a)
TypeError: object of type 'int' has no len()
有人能告诉我这个错误发生的原因吗?正确的代码应该是什么?在