“样本大于人口”错误并绕过相同的列表条目

时间:2015-04-22 01:01:55

标签: python-2.7 if-statement error-handling while-loop restart

我正在尝试创建一个交互式秘密圣诞老人选择器但是无论何时代码一直没有随机选择的名称是相同的,我得到一个错误,说'样本大于人口'。我该如何解决?此外,当我得到相同的名称,我希望循环重新开始。我怎么能这样做?

这是我到目前为止所拥有的......

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!'

另外,请记住我是python的初学者。

0 个答案:

没有答案