mardi 21 avril 2015

"sample larger than population" error and getting around same list entries

I' m trying to create an interactive secret santa picker but whenever the code goes all the way through without the randomly chosen names being the same I get an error saying 'sample larger than population'. How do I fix this? Also when I do get the same name, I want the loop to start over. How can I do this?

Here is what I have so far....

import random

def SecretSantaW():
    print ''
    print 'Welcome to the Secret Santa Picker!'
    nump = raw_input('How many people will be participating? ')
    nump = int(nump)
    groupG = []
    groupR = []
    while len(groupG) < nump:
        name = raw_input('Name? ')
        groupG.append(name)
        groupR.append(name)
    gsize = len(groupG)
    print ''

    while gsize > 0:
        rpplG = random.sample(groupG, 1) #selects 1 entry from giver list
        rpplR = random.sample(groupR, 1) #selects 1 entry from receiver list
        if rpplG[0] != rpplR[0]:
            print rpplG[0] + ', your are ' + rpplR[0] + '\'s Secret Santa!'
            if rpplG[0] in groupG:
                groupG.remove(rpplG[0])
                print groupG
            if rpplR[0] in groupR:
                groupR.remove(rpplR[0])
                print groupR
        else:
            pass


restart = True
while restart:
    SecretSantaW()
    print ''
    restart = raw_input('Would you like play again? [yes/no] ') == 'yes'

print ''
print 'Merry Christmas!'

Also, keep in mind I am a beginner with python.

Aucun commentaire:

Enregistrer un commentaire