python nonetype iterable,Python-“'NoneType'对象不可iterable”

我在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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值