Python中的中断循环

时间:2018-12-10 04:44:30

标签: python-3.x loops

我正在学习python(版本3),并且您可能已经知道,练习之一是创建一个石头剪刀布蜥蜴spock(rpsls)游戏,我被要求从下面的if语句开始,然后修改代码以包含循环并使用随机函数添加计算机播放器。我花了几天时间调整代码并进行谷歌搜索,但是我无法解决循环中断问题。它卡住了,不断要求玩家1输入,并且永远不会加载与玩家2的比较或完成回合。我意识到这是一种编写游戏代码的麻烦方式,但我想尽可能保留格式。

        import random

print("")
print("**** Welcome to Rock Paper Scissors ****")
print("")



inputOK = False
player2choices = ['rock', 'paper', 'scissors', 'lizard', 'spock']
while inputOK == False:
    stringPlayer1 = input("Player 1, choose: rock, paper, scissors, lizard, or spock: ")

    stringPlayer2 = random.choice(player2choices)
if stringPlayer1 == stringPlayer2:
    print("Tie: Both players chose:" +
          stringPlayer1)

elif stringPlayer1 == 'scissors' and stringPlayer2 == 'paper':
    print("Player 1 wins: scissors cuts paper.")

elif stringPlayer1 == 'paper' and stringPlayer2 == 'rock':
    print("Player 1 wins: paper covers rock.")

elif stringPlayer1 == 'rock' and stringPlayer2 == 'lizard':
    print("Player 1 wins: rock crushes lizard.")

elif stringPlayer1 == 'lizard' and stringPlayer2 == 'spock':
    print("Player 1 wins: lizard poisons spock.")

elif stringPlayer1 == 'spock' and stringPlayer2 == 'scissors':
    print("Player 1 wins: Spock smashes scissors.")

elif stringPlayer1 == 'scissors' and stringPlayer2 == 'lizard':
    print("Player 1 wins: scissors decapitates lizard.")

elif stringPlayer1 == 'lizard' and stringPlayer2 == 'paper':
    print("Player 1 wins: lizard eats paper.")

elif stringPlayer1 == 'paper' and stringPlayer2 == 'spock':
    print("Player 1 wins: paper disproves Spock.")

elif stringPlayer1 == 'spock' and stringPlayer2 == 'rock':
    print("Player 1 wins: Spock vaporizes rock.")

elif stringPlayer1 == 'rock' and stringPlayer2 == 'scissors':
    print("Player 1 wins: rock crushes scissors.")

elif stringPlayer1 == 'paper' and stringPlayer2 == 'scissors':
    print("Player 2 wins: scissors cuts paper.")

elif stringPlayer1 == 'rock' and stringPlayer2 == 'paper':
    print("Player 2 wins: paper covers rock.")

elif stringPlayer1 == 'lizard' and stringPlayer2 == 'rock':
    print("Player 2 wins: rock crushes lizard.")

elif stringPlayer1 == 'spock' and stringPlayer2 == 'lizard':
    print("Player 2 wins: lizard poisons spock.")

elif stringPlayer1 == 'scissors' and stringPlayer2 == 'spock':
    print("Player 2 wins: Spock smashes scissors.")

elif stringPlayer1 == 'lizard' and stringPlayer2 == 'scissors':
    print("Player 2 wins: scissors decapitates lizard.")

elif stringPlayer1 == 'paper' and stringPlayer2 == 'lizard':
    print("Player 2 wins: lizard eats paper.")

elif stringPlayer1 == 'spock' and stringPlayer2 == 'paper':
    print("Player 2 wins: paper disproves Spock.")

elif stringPlayer1 == 'rock' and stringPlayer2 == 'spock':
    print("Player 2 wins: Spock vaporizes rock.")

elif stringPlayer1 == 'scissors' and stringPlayer2 == 'rock':
    print("Player 2 wins: rock crushes scissors.")

else:
    inputOK = False
    print("Error: Not a valid choice.")

quit = input("Do you want to quit? ")
if quit.lower() == "y" or quit.lower() == "yes":
    done = True

2 个答案:

答案 0 :(得分:1)

while循环将继续循环直到您的变量inputOk等于True。您的错误是,当玩家想要结束游戏时,inputOK没有更新为True。也许您打算将inputOk设置为True而不是完成?

quit = input("Do you want to quit? ") if quit.lower() == "y" or quit.lower() == "yes": inputOk = True

编辑:同样如前所述,您必须缩进python。在while语句下未缩进的任何代码都不会循环。

答案 1 :(得分:1)

用户将陷入无限循环,除非在while循环中的某个地方通过设置inputOK = True来更改条件。

在此之前,您需要确定有效输入的条件,例如用户输入是有效选择之一(if stringPlayer1 in player1choices

player2choices = ['rock', 'paper', 'scissors', 'lizard', 'spock']
player1choices = player2choices

while inputOK == False:
    stringPlayer1 = input("Player 1, choose: rock, paper, scissors, lizard, or spock: ")
    if stringPlayer1 in player1choices: # a condition for valid input?
        inputOK = True
    stringPlayer2 = random.choice(player2choices)

那至少应该解决这个破环,并使您可以继续开发游戏。