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