我是一名初级程序员,我需要一些代码方面的帮助。我目前正在创建一个掷骰子游戏模拟器,但似乎我的代码不会真正运行。附上我的代码和一些注意事项,每次掷骰子时,用户必须按回车键,这将导致程序掷骰子。
为了简要概述,以下是掷骰子背后的一些规则:
每轮有两个阶段:“出来”和“点”。为了开始一轮,射手进行一次或多次“出局”掷骰。出局掷出 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!"