我是一名初学者程序员,我需要帮助我的代码的一些部分。我目前正在创建一个掷骰子游戏模拟器,但看起来我的代码不会真正运行。我的代码附加在一些笔记中,每次掷骰子时,用户都必须按回车,这会导致程序掷骰子。初学者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!"
“我的问题是,为什么我的程序没有完全运行,以及缺陷在哪里。”你应该告诉我们为什么你的程序没有运行,缺陷在哪里。那不是我们的工作。 – Shashank
对不起,我在这里还是新手,我正在学习如何更好地提出这些问题。不会再发生,我意识到这是为了获得帮助和学习。 –