首页 > 解决方案 > 我收到错误 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

标签: pythontypeerrorrepl.it

解决方案


numbers是一个列表,您不能将模与仅用于整数的列表一起使用。

if (numbers % 2) == 1:

应该看起来像:

if (numbers[b] % 2) == 1:

旁注:在大多数编程语言中,数组/列表从 0 开始。


推荐阅读