初学者Python:Craps Game,while循环实现

时间:2013-10-10 03:04:52

标签: python

我是初学程序员,我需要帮助处理代码的某些部分。我目前正在创建一个掷骰子游戏模拟器,但似乎我的代码不会真正运行。我的代码是附加的,并且对于一些注释,每次掷骰子时,用户必须按Enter键才能使程序滚动骰子。

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

  

每一轮都有两个阶段:“出来”和“点”。开始一轮,   射手制作一个或多个“出来”的卷。推出2卷,   3或12输了,被称为“废话”。一个7或11的出来的卷(a   “自然”胜利。其他可能的数字是点数: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!"

2 个答案:

答案 0 :(得分:0)

某些错误消息可能会对读者有所帮助。

我认为您的问题是,您应该print当前正在使用return的所有地方。 请参阅Why would you use the return statement in Python?

其他说明:if rollDice == rollDice:永远都是真的 - 为什么要包括它?

答案 1 :(得分:0)

对于初学者来说,一个return语句会脱离它所处的函数。所以

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

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

我回应弗雷德里克关于if rollDice == rollDice部分的评论,根本不需要把它放到if语句中,因为它总会运行。

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