我在codecademy项目中的指令是Define a function called purify that takes in a list of numbers, removes all odd numbers in the list, and returns the result. For example, purify([1,2,3]) should return [2]. Do not directly modify the list you are given as input; instead, return a new list with only the even numbers.
我想出的密码是:def purify(numbers):
pure = numbers
for num in pure:
if num % 2 != 0:
pure = pure.remove(num)
return pure
错误是:Oops, try again. Your function crashed on [1] as input because your function throws a "'NoneType' object is not iterable" error.
据我所知,这意味着代码中的某些内容将被返回为“None”。或者没有数据。我似乎找不到这个简短的代码有什么问题,除非它在我的if语句中。提前谢谢。
所以我在代码中进行了编辑以删除“pure=pure.remove(num)”,这解决了none问题。但是,当运行代码时,仍然无法输入[4,5,5,4]并返回[4,5,4]。
新代码:def purify(numbers):
pure = numbers
for num in pure:
if num % 2 != 0:
pure.remove(num)
return pure