how to find the average number of rolls it takes to complete a snakes and ladders game?

后端 未结 2 1269
执念已碎
执念已碎 2021-01-22 01:56

write code to simulate the game for one player and calculate the number of dice throws required to finish a game. The user should be allowed to specify the number of games simul

2条回答
  •  无人及你
    2021-01-22 02:43

    The code that you have written only rolls the dice one time. You need to keep rolling until you have won the game. It might be best to create a function that plays the game one time and returns the number of dice rolls needed.

    def playGame():
        counterPosition = 0
        numberOfRolls = 0
        while (counterPosition < 100):
            numberOfRolls += 1
            currentDiceroll = diceroll()
            print("The currentDiceroll is", currentDiceroll)
            counterPosition += currentDiceroll
            # your if statements go here
        return numberOfRolls
    

    Then you can call this function however many times you want.

    totalDiceRolls = 0
    for i in range(userInput):
        totalDiceRolls += playGame()
    avgRolls = totalDiceRolls / userInput
    

提交回复
热议问题