首页 > 解决方案 > 我在石头剪刀布游戏中的 while 循环不起作用。我应该如何纠正它?

问题描述

当人类或计算机达到 5 分时,游戏应该结束。这个游戏以前只是一个功能。然后我添加了输入函数和 while 循环,以使其更高效和用户友好。

import random

choices=('rock','paper','scissor')
HUMAN_SCORE=0
COMPUTER_SCORE=0


while COMPUTER_SCORE<5 or HUMAN_SCORE<5:
    computer=random.choice(choices)
    human=input("Choose from rock,paper or scissor")
    print("You picked:")
    print(human)
    print("Computer picked:")
    print(computer)
    if human == "rock" and computer == "rock":
        print("play again")
    elif human == "rock" and computer == "paper":
        COMPUTER_SCORE=COMPUTER_SCORE+1
        print("sorry,you lost!Better luck next time!")
    elif human == "rock" and computer == "scissor":
        HUMAN_SCORE=HUMAN_SCORE+1
        print("Congratulations,you won!")
    elif human == "paper" and computer == "paper":
        print("play again")
    elif human == "paper" and computer == "scissor":
        COMPUTER_SCORE = COMPUTER_SCORE + 1
        print("sorry,you lost!Better luck next time!")
    elif human == "paper" and computer == "rock":
        print("Congratulations,you won!")
        HUMAN_SCORE = HUMAN_SCORE + 1
    elif human == "scissor" and computer == "scissor":
        print("play again")
    elif human == "scissor" and computer == "rock":
        print("sorry,you lost!Better luck next time!")
        COMPUTER_SCORE = COMPUTER_SCORE + 1
    elif human == "scissor" and computer == "paper":
        print("Congratulations,you won!")
        HUMAN_SCORE = HUMAN_SCORE + 1
    else:
        print("try again!Choose from the options above!")
    print("Human_Score=")
    print(HUMAN_SCORE)
    print("Computer_Score=")
   `print(COMPUTER_SCORE)

标签: python

解决方案


while COMPUTER_SCORE<5 or HUMAN_SCORE<5:

很可能这总是正确的(人或计算机不能同时击中 5,所以必须是正确的)

你需要确保它们都小于 5and

while COMPUTER_SCORE<5 and HUMAN_SCORE<5:

推荐阅读