我是一名初學者程序員,我需要幫助我的代碼的一些部分。我目前正在創建一個擲骰子游戲模擬器,但看起來我的代碼不會真正運行。我的代碼附加在一些筆記中,每次擲骰子時,用戶都必須按回車,這會導致程序擲骰子。初學者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
對不起,我在這裏還是新手,我正在學習如何更好地提出這些問題。不會再發生,我意識到這是爲了獲得幫助和學習。 –