Reputation: 1134
I'm having one of 'those' days, and everything I'm looking at is just messing up.
I have a Die class (as in Dice) and I want to roll 100 of them, and count the occurrences of each number. Say, out of 100 dice, 40 were the number '6'.
The issue is, if I hardcode a if face_value == 1 type of solution - if someone changes the amount of faces on the die - I've got a bit of an issue - as it won't count them.
I thought the best way to account for this would be to make a list (which is the same size as the possible die faces) and increment the number inside the list on each occurrence.
In short, how can I increment the value inside of each list element?
diceList = [ Die() for q in range(100)]
import random class Die: face_value = 1 size = 6 def getSize(self): return self.size def getFaceValue(self): return self.face_value def roll(self): self.face_value = random.randint(1, self.size)
def rollList(n): i = 0 a = n[0].getSize() faceList = [0] * a for Die in n: n[i].roll() x = n[i].getFaceValue() print(n[i].face_value) #faceList[x-1] = 1+1 #print(faceList) i += 1
I've re-worked it a little, and I've got a way of getting the answer - I'm not sure if it's a particularly good way of doing it though.
def rollList(n): i = 0 faceList = [] a = n[0].getSize() for Die in n: n[i].roll() x = n[i].getFaceValue() faceList.append(x) i += 1 print(faceList) c = 1 while a >= c: print(faceList.count(c)) c += 1
Upvotes: 2
Views: 13726
Reputation: 659
def rollList(n):
a = n[0].getSize()
faceList = [0] * a
for d in n:
d.roll()
x = d.getFaceValue()
faceList[x-1] += 1
Upvotes: 1