0

我是一名初级程序员,我需要一些代码方面的帮助。我目前正在创建一个掷骰子游戏模拟器,但似乎我的代码不会真正运行。附上我的代码和一些注意事项,每次掷骰子时,用户必须按回车键,这将导致程序掷骰子。

为了简要概述,以下是掷骰子背后的一些规则:

每轮有两个阶段:“出来”和“点”。为了开始一轮,射手进行一次或多次“出局”掷骰。出局掷出 2、3 或 12 输,称为“废话”。7 或 11(“自然”)的出局获胜。其他可能的数字是点数:4、5、6、8、9 和 10。如果射手在出局掷骰时掷出这些数字中的一个,这就确立了“点”并继续到点阶段。在点数阶段,如果用户掷出的数字与出局阶段相同,则他们“击中”或赢得游戏。但是,如果他们掷出 7,他们就会“七出局”或输掉比赛。如果玩家没有得到 7 或相同的出局号码,他们将继续滚动,直到他们击中或出局七。

我的问题是,当程序运行时,我可以得到请按回车,但是当我按回车时,它不会继续到将掷骰子的下一部分代码。我不太明白为什么会这样。此外,我可能需要一些帮助来查看我的代码背后的逻辑,我不完全确定在实施时是否会出现预期的结果。任何帮助表示赞赏!

import random

def playRound():

    #This will print out the current phase.

    print "The come-out phase:"
    print 

    #This will tell the user to hit enter to roll the dice.

    rollDice = raw_input("Hit ENTER to roll the dice...")

    #this will ensure that when a user hits enter, the program moves on.

    if rollDice == rollDice:

        #this will sum up two random integers to simulate two dice being thrown and record         the total.

        diceTotal = random.randint(1,6) + random.randint(1,6)

        #this will see if the numbers are 7 or 11, and if so, will let the user know they won.

        if diceTotal in (7,11):

            return "You rolled a", diceTotal
            return "You Win: Natural!"

        #this will see if numbers are 2, 3, or 12, and if so, will let user know they lost.

        if diceTotal in (2,3,12):

            return "You rolled a", diceTotal
            return "You Lose: Crap-Out!"

        #let user know what they rolled if conditions above are not met.

        return "You Rolled a", diceTotal

        #This will now start the point phase.

        print "The Point Phase:"
        print

        #this will ask the user to hit enter to roll the dice.

        rollDice = raw_input("Hit ENTER to roll the dice...")

        #this will ensure that when the user hits enter, the dice will roll.

        if rollDice == rollDice:

            #this will add up the sum of two random numbers simulating dice and save to variable.

            diceTotalPoint = random.randint(1,6) + random.randint(1,6)

            #this will see if the roll is not 7 or the diceTotal from come-out phase.

            while diceTotalPoint not in (7, diceTotal):

                #This will continue to roll the dice, if 7 or the come-out phase number is not achieved.

                rollDice = raw_input("Hit ENTER to roll the dice...")

                if rollDice == rollDice:

                    diceTotalPoint = random.randint(1,6) + random.randint(1,6)

            #this will tell the user that if the dice roll is the same as the come-out phase,           it will be a hit and they win.

            if diceTotalPoint == diceTotal:

                return "You Rolled a", diceTotalPoint
                return "You Win: Hit!"

            #this will tell the user if they get a 7, and tell them they lose.

            if diceTotalPoint == 7:

                return "You Rolled a", diceTotalPoint
                return "You lose: Seven-Out!"
4

2 回答 2

0

一些错误信息可能对读者有所帮助。

我认为你的问题是你应该print在你当前使用的任何地方return。请参阅为什么要在 Python 中使用 return 语句?.

其他注意事项:if rollDice == rollDice:将永远是真实的 - 为什么还要包括它?

于 2013-10-10T03:26:28.343 回答
0

对于初学者来说,return 语句退出了它所在的函数。所以

        return "You rolled a", diceTotal
        return "You Lose: Crap-Out!"

永远不会到达“你输了:废话!” 因为它跳过了第一个返回值。请改用打印。

我赞同 Frederick 关于 ifrollDice == rollDice部分的评论,根本不需要将其放入 if 语句中,因为它会一直运行。

总的来说,这是一个功能强大的野兽。我建议将其拆分为多个函数,或者更好的类以使事情更易于管理。现在它是一个神功能(http://iwanttocode.wordpress.com/tag/god-function/),它只是乞求维护或调试的痛苦。如果您想为另一个骰子游戏编写另一个程序,还请考虑您发布的代码如何有 0 个可重用代码。

于 2013-10-10T03:46:29.053 回答