首页 文章

Python中的断循环

提问于
浏览
-4

我正在研究python(版本3),并且你可能知道,其中一个练习是创建一个岩石剪刀蜥蜴spock游戏(rpsls)我需要从下面的if语句开始,然后修改代码包含循环并使用随机函数添加计算机播放器 . 我花了几天时间调整代码和google搜索的东西,但是我还没能在循环中修复中断 . 陷入困境,要求玩家一个无休止地输入,并且永远不会加载与player2的比较或完成回合 . 我意识到这是一种编码游戏的混乱方式,但我想尽可能保留格式 .

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

    用户将陷入无限循环,除非在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)
    

    这应该至少可以修复破碎的循环并允许您更多地继续开发游戏 .

  • 0

    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语句下缩进的代码都不会循环 .

相关问题