如何在Python中停止此循环?

时间:2018-10-07 20:26:53

标签: python function loops if-statement break

这完全符合我的期望,但是当用户输入无效选项时我无法停止它。这是一款石头剪刀布游戏,它不仅记录用户的输入,还保留当前回合的分数,并保留所有回合的最终分数,直到游戏结束为止……就目前而言,从未发生过。当用户输入无效选项时,如何结束此游戏?我尝试使用break,但无效。

required

4 个答案:

答案 0 :(得分:1)

如果返回的分数都等于零,则玩家输入了错误的输入,您可以中断循环。

while True:
    player, computer = rock_paper_scissors()
    if player == 0 and computer == 0:
        break
    playerFinal += player
    computerFinal += computer
    print("Your score:", str(playerFinal) + ", Computer score:", computerFinal)

答案 1 :(得分:1)

这可以通过简单地添加一个标志来检查while True循环是否需要结束来解决。 在这里:

import random
def rock_paper_scissors():
    playerScore = 0
    computerScore = 0
    flag = False

    print("")

    player = input("Choose Rock, Paper, or Scissors: ")
    player = player.lower()

    choices = ["rock", "paper", "scissors"]

    computer = random.choice(choices)


    if player == computer:
        print("I chose " + str(computer) + " and you chose " + player + ". It's a tie!")
    elif player == "rock" and computer == "scissors":
        playerScore += 1
        print("I chose " + str(computer) + " and you chose " + player + ". Congratulations! You won! " + player + " beats " + str(computer) + ".")
    elif player == "paper" and computer == "rock":
        playerScore += 1
        print("I chose " + str(computer) + " and you chose " + player + ". Congratulations! You won! " + player + " beats " + str(computer) + ".")
    elif player == "scissors" and computer == "paper":
        playerScore += 1
        print("I chose " + str(computer) + " and you chose " + player + ". Congratulations! You won! " + player + " beats " + str(computer) + ".")
    elif computer == "rock" and player == "scissors":
        computerScore += 1
        print("I chose " + str(computer) + " and you chose " + player + ". You lost! " + str(computer) + " beats " + player + ".")
    elif computer == "paper" and player == "rock":
        computerScore += 1
        print("I chose " + str(computer) + " and you chose " + player + ". You lost! " + str(computer) + " beats " + player + ".")
    elif computer == "scissors" and player == "paper":
        computerScore += 1
        print("I chose " + str(computer) + " and you chose " + player + ". You lost! " + str(computer) + " beats " + player + ".")
    else:
        flag = True
        print("Sorry, but you entered an invalid option.  The game has ended.  See below for the final score.  Thank you for playing")
        print("")
        print("Your score:", str(playerScore) + ", Computer score:", str(computerScore))

    return playerScore, computerScore, flag

playerFinal = 0
computerFinal = 0

while True:
    player, computer, flag = rock_paper_scissors()
    playerFinal += player
    computerFinal += computer
    print("Your score:", str(playerFinal) + ", Computer score:", computerFinal)
    if flag:
        break

答案 2 :(得分:0)

只需在无效选择后添加break 您可以在那时让播放器减负滚动

if player < 0
   playerFinal = -1 * player
   break

答案 3 :(得分:0)

从以下位置更改循环条件:

while True:

收件人:

while True and (player+computer) != 0 :

用户选择无效时,该回合的得分将为0,而下一次循环将不通过该条件。