Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python初学者:骰子游戏,while循环实现_Python - Fatal编程技术网

Python初学者:骰子游戏,while循环实现

Python初学者:骰子游戏,while循环实现,python,Python,我是一个初学者程序员,我需要一些代码的帮助。我目前正在创建一个垃圾游戏模拟器,但我的代码似乎不会真正运行。我的代码是附加的,对于一些注意事项,每次有骰子滚动,用户必须点击回车键,这将导致程序滚动骰子 以下是垃圾背后的一些规则,简要概述一下: 每轮比赛分为两个阶段:“出局”和“得分”。开始一轮,, 射手打出一个或多个“出局”掷骰。一卷2个, 3或12输了,被称为“废话”。一卷7或11(A) “自然”获胜。其他可能的数字是点数:4, 5、6、8、9和10。如果射手在屏幕上掷下其中一个数字 出来滚,这

我是一个初学者程序员,我需要一些代码的帮助。我目前正在创建一个垃圾游戏模拟器,但我的代码似乎不会真正运行。我的代码是附加的,对于一些注意事项,每次有骰子滚动,用户必须点击回车键,这将导致程序滚动骰子

以下是垃圾背后的一些规则,简要概述一下:

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

我的问题是,当程序运行时,我能够得到Please hit enter,但当我点击enter时,它将不会继续到下一部分代码,而这部分代码将掷骰子。我不太明白为什么会这样。另外,我可能需要一些帮助来查看代码背后的逻辑,我不完全确定在实现时是否会出现预期的结果。感谢您的帮助

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!"

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

我认为您的问题是,您应该
打印当前使用的
返回的所有地方。
看


其他注意事项:
如果rollDice==rollDice:
将始终为真-为什么还要包括它?

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

        return "You rolled a", diceTotal
        return "You Lose: Crap-Out!"
永远不会出现“你输了:滚吧!”的情况,因为它会在第一次返回值时跳出。改用打印

我附和弗雷德里克关于if
rollDice==rollDice
部分的评论,根本不需要将其放入if语句中,因为它将始终运行


总的来说,这是一个功能的野兽。我建议将其拆分为多个函数,或者更好的方法是将其拆分为类,以便更易于管理。现在它是一个god函数(),它只是在乞求痛苦的wrt维护或调试。另外,想想如果你想为另一个骰子游戏编写另一个程序,那么你发布的代码中有0个可重用代码。

“我的问题是,为什么我的程序不能完全运行,缺陷在哪里。”你应该告诉我们为什么你的程序不能运行,缺陷在哪里。那不是我们的工作。抱歉,我还是新来的,我正在学习如何更好地问这些问题。不会再发生了,我意识到这是为了得到帮助和学习。