python - 我收到错误 TypeError: Unsupported operand type(s) for %: 'list' and 'int'
问题描述
我是 python 新手,但我收到此错误并且不知道如何解决它。
numbers = [100, 50, 10, 1, 2, 7, 11, 17, 53, -8, -4, -9, -72, -64, -80]
b=1
while b>=1:
if (numbers % 2) == 1:
print(numbers[b])
b += 2
else:
break
解决方案
numbers
是一个列表,您不能将模与仅用于整数的列表一起使用。
if (numbers % 2) == 1:
应该看起来像:
if (numbers[b] % 2) == 1:
旁注:在大多数编程语言中,数组/列表从 0 开始。
推荐阅读
- javascript - Javascript - 从树中递归删除某种类型的节点,但重新附加并传播符合条件的子节点
- javascript - 我无法验证此选择
- android - 小米 8 切换应用视图 Flutter 应用名称错误
- excel - 确定单元格地址以了解我的循环在哪里结束 - EXCEL VBA
- python - 循环问题在Python中,没有定义循环
- mysql - 多个 connection.query()-s 会减慢 node.js/mysql 中的连接吗?
- python - 尝试在 Python 上以灰度显示图像时出现 TyperError
- python - NameError:在文件目录中,我正在尝试加载数据进行分析
- bootstrap-4 - 如何强制 div 调整自身大小以包含折叠的元素
- python - 使用 PyImgur 记录的上传方式,uploads 拒绝显示在画廊中