如何退出while循环 - 当counterPosition> = 100开始新游戏时

时间:2017-01-14 19:46:40

标签: python

编写代码以模拟一个玩家的游戏并计算完成游戏所需的骰子投掷次数。应该允许用户指定模拟的游戏数量,代码应该计算每场比赛掷骰子的平均数量。

import random

def diceRoll():
    return random.randint (1,6)

userInput = int(input("How many games would you like to play snakes and ladders?"))

totalDiceRolls = 0

for i in range(userInput):

counterPosition = 0

while (counterPosition < 100):
    totalDiceRolls += 1
    currentDiceRoll = diceRoll()
    counterPosition += currentDiceRoll

    if counterPosition == 1:
        counterPosition = counterPosition + 37
    if counterPosition == 4:
        counterPosition = counterPosition + 10
    if counterPosition == 9:
        counterPosition = counterPosition + 22
    if counterPosition == 21:
        counterPosition = counterPosition + 21
    if counterPosition == 28:
        counterPosition = counterPosition + 56
    if counterPosition == 51:
        counterPosition = counterPosition + 16
    if counterPosition == 72:
        counterPosition = counterPosition + 19
    if counterPosition == 80:
        counterPosition = counterPosition + 19
    if counterPosition == 17:
        counterPosition = counterPosition - 10
    if counterPosition == 54:
        counterPosition = counterPosition - 20
    if counterPosition == 63:
        counterPosition = counterPosition - 4
    if counterPosition == 64:
        counterPosition = counterPosition - 4
    if counterPosition == 87:
        counterPosition = counterPosition - 51
    if counterPosition == 92:
        counterPosition = counterPosition - 19
    if counterPosition == 95:
        counterPosition = counterPosition - 20
    if counterPosition == 98:
        counterPosition = counterPosition - 19
    if counterPosition >= 100:


avgRolls = totalDiceRolls / userInput
print("The average number of rolls per game is", avgRolls)

1 个答案:

答案 0 :(得分:0)

您在上一个问题/答案中更改了代码中的所有内容,现在它无法正常工作。

您的playGame():圈内有while圈,但您将while移到了外面,但是您没有移动剩余的元素。

以前playGame():返回骰子数量,您可以将其添加到totalDiceRolls

totalDiceRolls += playGame()

但现在playGame()返回None,您会收到错误:

TypeError: unsupported operand type(s) for +=: 'int' and 'NoneType'

工作代码 - 没有playGame()

import random

def diceRoll():
    return random.randint (1,6)

userInput = int(input("How many games would you like to play snakes and ladders?"))

totalDiceRolls = 0

for i in range(userInput):

    counterPosition = 0 #setting counterposition and diceroll to 0

    while (counterPosition < 100):
        totalDiceRolls += 1
        currentDiceRoll = diceRoll()
        print("The currentDiceroll is", currentDiceRoll)

        counterPosition += currentDiceRoll

        if counterPosition == 1: #all the if statements show what happens if the one player lands on a snake or a ladder
            counterPosition = counterPosition + 37
        if counterPosition == 4:
            counterPosition = counterPosition + 10
        if counterPosition == 9:
            counterPosition = counterPosition + 22
        if counterPosition == 21:
            counterPosition = counterPosition + 21
        if counterPosition == 28:
            counterPosition = counterPosition + 56
        if counterPosition == 51:
            counterPosition = counterPosition + 16
        if counterPosition == 72:
            counterPosition = counterPosition + 19
        if counterPosition == 80:
            counterPosition = counterPosition + 19
        if counterPosition == 17:
            counterPosition = counterPosition - 10
        if counterPosition == 54:
            counterPosition = counterPosition - 20
        if counterPosition == 63:
            counterPosition = counterPosition - 4
        if counterPosition == 64:
            counterPosition = counterPosition - 4
        if counterPosition == 87:
            counterPosition = counterPosition - 51
        if counterPosition == 92:
            counterPosition = counterPosition - 19
        if counterPosition == 95:
            counterPosition = counterPosition - 20
        if counterPosition == 98:
            counterPosition = counterPosition - 19
        if counterPosition >= 100:
            print ("Congratulations end of game")

avgRolls = totalDiceRolls / userInput
print("The average number of rolls per game is", avgRolls)