Domanda

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)
È stato utile?

Soluzione

for x in range (6):

try this:

for x in range (1, 7):

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top