문제

I'm making a game where a user rolls a dice, this works fine and then i calculate how many times each number has been rolled in my countVals function, however it counts incorrectly it counts 1 digit off.

if dice rolls 5,1,3,1,5

Expected result of counting would be 2,0,1,0,2 as it counts how many times each digit has occured

Actual result currently is 0,2,0,1,0,2

Code is below

for x in range (6):
    #counter = dice.count(x)
    print(dice.count(x)
도움이 되었습니까?

해결책

for x in range (6):

try this:

for x in range (1, 7):

다른 팁

It looks like your ranges are off (they need to be half-open intervals).

  randrange(1, 7)       # die roll is one of {1,2,3,4,5,6}

The counting should likewise loop over:

  for x in range(1, 7):
      print(dice.count(x))

Change the range to:

random.randrange(1,7,1)

Remember parameters are:

randrange(start, end, step)

and it returns a value between [0, end-1] with a specific step.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top