如何在python中继续while循环



我正在构建一个python骰子游戏,其中两个玩家应该玩一个vs.代码运行正确,第一个玩家能够玩他/她的回合,但是我有一点问题通过while语句循环,以便使第二个玩家玩他/她的回合。我想我会添加一个continue在if语句(在代码的末尾),但它说"continue" can be used only within a loop这是我的游戏代码

import random
import array as arr
# enter player details:
player = ["", ""]
player[0] = input("Player one name :")
player[1] = input("Player two name :")
print("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
print(player[0], "vs", player[1])
# waiting for user to press enter key
input("Press enter key to continue...")
# clearing screen
# creating an array of 5 dice with integer type
dice = arr.array('i', [1, 2, 3, 4, 5])
k = 0
busted = 0
round = 1
score = 0
roll = 0
possible_output = [1, 2, 3, 4, 5, 6]
print("round : ", round)
while busted == 0 or score < 10000 or round < 6:
# filling each value of dice array with random number from 1 to 6
for j in range(0, 5):
# rolling dice
dice[j] = random.choice(possible_output)
break
print(dice)      
roll += 1
# calculating score
# condition when there is a series of 1 through 5
if dice[0] == 1 and dice[1] == 2 and dice[2] == 3 and dice[3] == 4 and dice[4] == 5:
score = score+1250
# condition when there is a series of 2 through 6
elif dice[0] == 2 and dice[1] == 3 and dice[2] == 4 and dice[3] == 5 and dice[4] == 6:
score = score+1250
# if value is a 1 or 5;
for j in range(0, 5):
if dice[j] == 1:
score = score+100
elif dice[j] == 5:
score = score+50
# printing the results
print("Result of dice rolling is :", dice)
print("Roll total : ", score)
roll_dice = input("[s]ave or [R]oll")
if roll_dice == "s":
print("saved!")
else:
if k == 0:
k = 1
continue 
else:
k = 0
round = round+1
if round == 6:
print("Thankyou, the game is over")

if块位于while块的外部(缩进)。你没有在while块中执行这个if,因此出现错误。

最新更新