Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

How do I write a code for a group random dating?

How do I write a code for a group random dating, and each time, there is no one duplicate in the list. Let say we have 10 people in a group, and we do 1 on 1 meeting. right now, every time I do a random pick in the list, some names duplicates.

let say I break the group into two list x and y there is something wrong with it still. Error Message: Traceback (most recent call last):
File "/home/treehouse/workspace/reviewfile.py", line 11, in <module>
d = random.choice(y)
File "/usr/local/lib/python3.9/random.py", line 346, in choice
return seq[self._randbelow(len(seq))]
IndexError: list index out of range

import random

x = ['a', 'b', 'c', 'd', 'e'] y = ['z', 'n', 'm', 'l', 'p']

while len(x)>0: z = random.choice(x) d = random.choice(y) print(z, d) x.remove(z) y.remove(d)

1 Answer

Kevin S
seal-mask
.a{fill-rule:evenodd;}techdegree
Kevin S
Data Analysis Techdegree Student 15,862 Points

Hmm strange, I ran your code as you wrote it, and it did not throw the error.

This was the code I ran:

import random

x = ['a', 'b', 'c', 'd', 'e']
y = ['z', 'n', 'm', 'l', 'p']

while len(x)>0:
    z = random.choice(x)
    d = random.choice(y)
    print(z, d)
    x.remove(z)
    y.remove(d)

And this was the output:

c l
e m
d z
b p
a n