2013-10-10 145 views
0

我是一名初学者程序员,我需要帮助我的代码的一些部分。我目前正在创建一个掷骰子游戏模拟器,但看起来我的代码不会真正运行。我的代码附加在一些笔记中,每次掷骰子时,用户都必须按回车,这会导致程序掷骰子。初学者Python:骰子游戏,while循环实现

对于一个简要概述,这里是一些掷骰子背后的规则:

每一轮有两个阶段:“来-out”和“点”。要开始一轮, 射手做一个或多个“出来”卷。一个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!" 
+1

“我的问题是,为什么我的程序没有完全运行,以及缺陷在哪里。”你应该告诉我们为什么你的程序没有运行,缺陷在哪里。那不是我们的工作。 – Shashank

+0

对不起,我在这里还是新手,我正在学习如何更好地提出这些问题。不会再发生,我意识到这是为了获得帮助和学习。 –

回答

0

对于初学者来说,return语句捞出它在功能所以

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

永远也不会去。“你输!胡扯出”因为它跳过了第一个返回值。改为使用打印。

我回应弗雷德里克关于如果rollDice == rollDice部分的评论,不需要将它放入if语句中,因为它总是会运行。

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