简体   繁体   中英

Python Error: TypeError "argument of type 'type' not iterable

I am working on a school project where I have to generate a list of numbers with no repeats. The twist is I am not allowed to used random.sample() or random.shuffle() . I think I have found a way around it with my code except I get the error TypeError "argument of type 'type' not iterable . I have not been able to get around this so I need some help. Thanks for the help Here is the code:

import random
lis=[]

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

print (lis)

Misspell in if rand not in list: , should be if rand not in lis:

Here is working code:

import random
lis=[]

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

print (lis)

It's a typo. Replace if rand not in list: lis.append(rand) with if rand not in lis: lis.append(rand)

Note list->lis

Change if rand not in list: lis.append(rand) to if rand not in lis: lis.append(rand)

To tell you why?

  • Checking in to a keyword list (which is not a iterable object)

  • Typo :-)

So you must want to check in the lis list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM