0
だから私はNBAの宝くじをシミュレートしようとしていると私はチームの配列を作成し、forループ内の各チームにその組み合わせを割り当てるしようとしたとき、私はこの行の境界エラーのうち、ポップインデックスを取得:ポップインデックスが範囲外のエラーを返すのはなぜですか?
combos.append(self.combinations.pop(randint(0,len(self.combinations))))
しかし、単に1つのチームアイテムにその組み合わせを割り当てるだけでエラーは発生しません。エラーは、その組み合わせを繰り返し割り当てようとすると発生します。
from Team import *
from LotteryMachine import *
"""Main class"""
lotteryMachine = LotteryMachine()
"""Initialize Teams"""
teams = [Team('Lakers', '81-0',1),Team('Wizards', '81-0',2)]
for team in teams:
team.combinations=
lotteryMachine.assignCombinations(team.numCombinations)
チームクラス:
class Team():
combination_dict={1:250, 2:199, 3:156, 4:119, 5:88, 6:63, 7:43, 8:28, 9:17, 10:11, 11:8, 12:7, 13: 6, 14:5}
def __init__(self, name, record, standing):
self.name=name
self.record = record
self.standing = standing
self.numCombinations= Team.combination_dict[self.standing]
def __str__(self):
return self.name
抽選機クラス:
from random import *
class LotteryMachine():
def __init__(self):
self.combinations = list(self.createCombinations([x for x in range(1,15)],4))
self.deleteRandom()
self.copyCombinations = self.combinations
def combination(self,n,k):
return int(self.factorial(n)/(self.factorial(k)*self.factorial(n-k)))
def factorial(self,n):
assert n>=0
if n==0 or n==1:
return 1
return n*self.factorial(n-1)
def createCombinations(self,elements,length):
for i in range(len(elements)):
if length==1:
yield [elements[i],]
else:
for next in self.createCombinations(elements[i+1:len(elements)],length-1):
yield [elements[i],]+next
def deleteRandom(self):
self.combinations.pop(randint(0,len(self.combinations)))
def assignCombinations(self,length):
combos=[]
for x in range(length):
combos.append(self.combinations.pop(randint(0,len(self.combinations))))
return combos
#error occurs in above line
def drawCombinations(self):
combos=[]
for x in range(3):
combos.append(self.copyCombinations.pop(randint(0, len(self.copyCombinations))))
return combos
def __str__(self):
return "NBA Lottery Machine"
実際には何も含まれていますか? –
はい、LotteryMachineが初期化されると組み合わせが初期化されます –