我试图将元素从列表a转移到列表b,我询问要转移多少个数字。它显示了以下错误:
'<=' not supported between instances of 'NoneType' and 'int'
我的工作还没有完成
a = [2,4,6,8,10,12]
b = []
transfer = int(input("how many numbers would you like to transfer:?"))
for i in a:
if transfer == 1:
b.append(i) <= 2
print(b)
b.append(i)
返回None
。因此,比较b.append(i) <= 2
会产生一个错误。如果您想根据列表的长度编写代码,可以使用len(b)
。比如:或者您可以这样做:
这将只使用旧列表} 作为新列表
a
的^{b
首先在将输入转换为int时,应该防止出错。有两种方法可以做到这一点
试试。。除了声明
^{}
现在谈谈主要任务。有三种方法可以解决这个问题
For循环使用^{}
列表理解:
列表切片:
相关问题 更多 >
编程相关推荐