建立一个Python骰子游戏,卡在最后一个函数上?(关闭)

2024-09-18 22:13:13 发布

您现在位置:Python中文网/ 问答频道 /正文

在学校,我们正在为Python设计一个骰子游戏,我似乎不知道如何完成最后一个功能。在游戏中,以西,你有14个骰子,你必须把所有的骰子都掷出去。你必须选择骰子的模式,然后你重新掷骰子,直到所有骰子都是相同的数字。一旦他们都一样,你就赢了比赛。这个特定的函数用来计算掷骰子的数量,在使用“game(debug=True)”命令后,返回获胜所需的掷骰子数,并重新掷骰子,直到您获胜,如下所示:

In []: game()
Out[]: 18
In []: game(debug=True)
Out[]: [3, 3, 3, 3, 3, 5, 3, 3, 3, 2, 5, 5, 4, 4]
       [3, 3, 3, 3, 3, 2, 3, 3, 3, 3, 3, 3, 3, 6]
       [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5]
       [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
       4

除了我已经完成的功能外,我还有8个功能:

^{pr2}$

我知道其他所有的函数都是正确的,因为我已经测试过了,它们工作得很好,但是有人知道我如何完成最后一个函数吗?下面是给我的启动程序代码:

def game(debug=False): 
    '''rolls 14 dice and repeats until getting an Ezee
    returns the number of rolls made.
    '''
    dice = first_roll()
    if debug:
        print dice
    # Anything else to do before you start iterating?
    while not won(dice):
        # What to do if you haven't won?
    # What to do once you've won?

以下是我目前掌握的代码:

def game(debug=False): 
    '''rolls 14 dice and repeats until getting an Ezee
    returns the number of rolls made.
    '''
    #Initialize
    dice = first_roll()
    target = find_mode(dice)
    rolls = 0
    if debug:
        print dice
    while won(dice):
        rolls=rolls+1
        if list_unmatched_dice(dice, target) == find_mode(dice):
            return rolls #Report/use
    while not won(dice):
        #Accumulate
        rolls=rolls+1
        if list_unmatched_dice(dice, target) != find_mode(dice):
            return rolls

谁能帮我一把,告诉我我做错了什么吗?我想弄清楚这一点,我很困惑。你的回答对我有很大帮助,非常感谢!在


Tags: to函数debug功能yougametargetif
1条回答
网友
1楼 · 发布于 2024-09-18 22:13:13

你需要这样做:

def game(debug=False): 
    '''rolls 14 dice and repeats until getting an Ezee
    returns the number of rolls made.
    '''
    dice = first_roll()
    if debug:
        print dice
    counter = 0
    while not won(dice):
        counter += 1
        reroll_many(dice)
        if debug:
            print dice

    print counter


if __name__ == '__main__':
    game()

相关问题 更多 >