简体   繁体   English

如何在 python 中继续 while 循环

[英]How to continue a while loop in python

I am building a python dice game where two players are supposed to play a vs. The code runs correctly and the first player is able to play his/her round however am having a bit of a problem looping through the while statement so as to make the second player play his/her round.我正在构建一个 python 骰子游戏,其中两个玩家应该玩 vs. 代码运行正确并且第一个玩家能够玩他/她的回合但是我在循环 while 语句时遇到了一些问题以便第二个玩家玩他/她的回合。 I thought I would add a continue at if statement(at the end of the code )but it says "continue" can be used only within a loop here is my code for the game我想我会在 if 语句(在代码的末尾)添加一个continue ,但它说"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")

Your if block is outside the while block (indentation).您的 if 块在 while 块之外(缩进)。 You are not executing this if within the while block, hence the error.如果在 while 块内,您不会执行此操作,因此会出现错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM