【问题标题】:Python Error: TypeError "argument of type 'type' not iterablePython错误:TypeError“类型'type'的参数不可迭代
【发布时间】:2018-10-18 04:09:17
【问题描述】:

我正在做一个学校项目,我必须生成一个没有重复的数字列表。扭曲是我不允许使用random.sample()random.shuffle()。我想我已经用我的代码找到了一种解决方法,除了我收到错误TypeError "argument of type 'type' not iterable。我无法解决这个问题,所以我需要一些帮助。谢谢您的帮助 代码如下:

import random
lis=[]

for i in range(5):
    rand=random.randint(1,10)
    if rand not in list: lis.append(rand)

print (lis)

【问题讨论】:

  • 在你的 if 中,list 应该是 lis

标签: python random


【解决方案1】:

if rand not in list: 中的拼写错误,应该是 rand not in lis:

这是工作代码:

import random
lis=[]

for i in range(5):
    rand=random.randint(1,10)
    if rand not in lis:
        lis.append(rand)

print (lis)

【讨论】:

    【解决方案2】:

    这是一个错字。将if rand not in list: lis.append(rand) 替换为if rand not in lis: lis.append(rand)

    笔记列表->lis

    【讨论】:

      【解决方案3】:

      if rand not in list: lis.append(rand) 更改为if rand not in lis: lis.append(rand)

      告诉你为什么?

      • 检查in 到关键字list(不是可迭代对象)

      • 错字:-)

      所以你一定要签入lis 列表。

      【讨论】:

        猜你喜欢
        • 2019-07-23
        • 2012-04-26
        • 1970-01-01
        • 2022-11-08
        • 2020-03-14
        • 2012-12-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多