[英]How do I repeat a while loop statement in python?
我正在學習 python 並且我正在制作一個 2 人擲骰子游戲,但我似乎無法讓玩家擲骰子或再玩一輪。 這是我寫的代碼。 如果有人可以幫助我更正代碼,我將不勝感激。 謝謝!
import random
import time
player_a = input("Player1, please type your name:")
time.sleep(1)
player_b = input("Player2, please type your name:")
time.sleep(1)
print("Welcome to Dice Roller", player_a, "and", player_b)
score_a = 0
score_b = 0
roll_a = False
roll_b = False
while not (roll_a and roll_b):
ans_a = input("Type roll to roll the dice:")
if ans_a == "roll":
roll_a = (random.randint(1, 6))
time.sleep(1)
print(player_a, "has rolled", roll_a)
else:
print("Please check you spelling")
time.sleep(2)
ans_b = input("Your turn to roll, type roll to roll the dice:")
if ans_b == "roll":
roll_b = (random.randint(1, 6))
time.sleep(1)
print(player_b, "has rolled", roll_b)
if roll_a == roll_b:
time.sleep(2)
print("Tie")
break
roll_a = False
roll_b = False
if roll_a > roll_b:
score_a = roll_a - roll_b
print(player_a, "has scored", score_a, "points")
break
roll_a = False
roll_b = False
if roll_b > roll_a:
score_b = roll_b - roll_a
print(player_b, "has scored", score_b, "points")
break
roll_a = False
roll_b = False
您可能希望將您的游戲重構為一個函數,該函數請求用戶輸入“roll”並返回 roll 結果,如下所示。
主while True
循環永遠不會結束(盡管您可以將其設置for round in range(5):
播放 5 輪),並且request_roll()
循環將隨着return
中斷。
import random
import time
def request_roll(name):
while True:
ans = input(f"{name}, type roll to roll the dice:").lower()
if ans == "roll":
roll = random.randint(1, 6)
time.sleep(1)
print(name, "has rolled", roll)
return roll
print("Please check your spelling")
time.sleep(2)
player_a = input("Player1, please type your name:")
player_b = input("Player2, please type your name:")
print("Welcome to Dice Roller", player_a, "and", player_b)
while True:
roll_a = request_roll(player_a)
roll_b = request_roll(player_b)
if roll_a > roll_b:
score_a = roll_a - roll_b
print(player_a, "has scored", score_a, "points")
elif roll_b > roll_a:
score_b = roll_b - roll_a
print(player_b, "has scored", score_b, "points")
else:
print("It's a draw")
break
語句立即退出 while 循環,因此第一個break
之后的代碼永遠不會運行。 有關更多信息,請參閱此處的文檔: https : //www.tutorialspoint.com/python/python_break_statement.htm
您的代碼還有其他幾個尚未顯示的問題(因為該部分代碼永遠不會運行):
在下一節中, roll_a
和roll_b
被False
覆蓋。 這意味着 if 語句中的比較將不起作用。 您還覆蓋了score_a
,我假設您想在其中添加(使用+=
運算符而不是=
)。
roll_a = False
roll_b = False
if roll_a > roll_b:
score_a = roll_a - roll_b
print(player_a, "has scored", score_a, "points")
我會建議如下(注意我沒有測試過!):
play_another_round = True
while play_another_round:
ans_a = input("Type roll to roll the dice:")
if ans_a == "roll":
roll_a = (random.randint(1, 6))
time.sleep(1)
print(player_a, "has rolled", roll_a)
else:
print("Please check you spelling")
time.sleep(2)
ans_b = input("Your turn to roll, type roll to roll the dice:")
if ans_b == "roll":
roll_b = (random.randint(1, 6))
time.sleep(1)
print(player_b, "has rolled", roll_b)
if roll_a == roll_b:
print("Tie")
elif roll_a > roll_b:
score_a += roll_a - roll_b
print(player_a, "has scored", score_a, "points")
elif roll_b > roll_a:
score_b += roll_b - roll_a
print(player_b, "has scored", score_b, "points")
continue_game_check = input("Do you want to play another round (Yes/No)?")
play_another_round = continue_game_check == "Yes"
print("The game has ended")
print(player_a, "has scored", score_a, "points")
print(player_b, "has scored", score_b, "points")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.