这是我的代码。我已经查看过许多类似的代码,并且我已经准确地设置了应该如何编写代码。我没有收到错误!
问题是我收到的输出是[11]。当用户输入[1,2,3,4,5,6,7,8,9,11]时。为什么只拉一个奇数?
totlist = []
max_int = 10
oddlist = []
while len(totlist) < max_int:
nums = int(input('Enter a number: '))
totlist.append(nums)
def find_odds(totlist, oddlist):
if len(totlist) == 0:
return
v = totlist.pop()
if v % 2 == 1:
oddlist.append(v)
find_odds(totlist,oddlist)
print(oddlist)
答案 0 :(得分:0)
你已经忘记了函数
中的循环boucledef find_odds(totlist, oddlist):
for item in range(len(totlist)) : # here
if len(totlist) == 0:
return
v = totlist.pop()
if v % 2 == 1:
oddlist.append(v)