简体   繁体   中英

Generating random numbers from a list while adding/removing terms

I'm writing a program where I have a "group" of 6 people trying to guess a number between 1 and 6. If the first person gets it right then 1 is added to their score, but if not the second person takes a guess. If they are right then 1 is added to their score and so on. However, the second person cannot guess the same number as the first person - this is the problem I've been having trouble with. For some reason I'm getting

TypeError: object of type 'NoneType' has no len()

I thought NoneType was some kind of variable thing but I'm not really sure. I'm not using the function len either. The code is below.

a = [1, 2, 3, 4, 5, 6]
judge = random.choice(a)
p1guess = random.choice(a) 
a = a.remove(p1guess)
p2guess = random.choice(a)
a = a.remove(p2guess)
p3guess = random.choice(a)
a = a.remove(p3guess)
p4guess = random.choice(a)
a = a.remove(p4guess)
p5guess = random.choice(a)
a = a.remove(p5guess)
p6guess = random.choice(a)

remove returns none and does the removal in place, don't reassign it back to a.

...
p1guess = random.choice(a) 
a.remove(p1guess)
p2guess = random.choice(a)
...

Otherwise, you can just shuffle a and assign them all at once,

a = [1, 2, 3, 4, 5, 6]
random.shuffle(a)
judge = random.choice(a)

p1guess, p2guess, p3guess, p4guess, p5guess, p6guess = a

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