石头,纸,剪刀-Python

每当我运行我的代码时,它都会给出正确的答案,但是,答案会不断重复,直到我终止程序。我会解决此问题。

import random

RPS = ("Rock", "Paper", "Scissors")
computer = random.choice(RPS)
Player = input("Enter Rock, Paper, or Scissors : ")
player = False

while player == False:
    if Player == computer:
        print("It is a tie")
    elif Player == "Rock":
        if computer == "Paper":
            print("You have lost,", computer, "covers", Player)
        else:
            print("You win,", player, "beats", computer)
    elif Player == "Scissors":
        if computer == "Rock":
            print("You have lost,", computer, "breaks", Player)
        else:
            print("You win,", Player, "beats", computer)
    elif Player == "Paper":
        if computer == "Scissors":
            print("You have lost,", computer, "cuts", Player)
        else:
            print("You win,", Player, "beats", computer)
    else:
        print("That is a invalid entry")
评论
  • 傻瓜Ina
    傻瓜Ina 回复

    You are looping while player == False. This is something you set before the loop starts, and do not modify at any point during the loop. As a result, player will always equal False. You can either set player = True during the loop, or utilize break.

    https://www.programiz.com/python-programming/break-continue

  • 终淡化了美
    终淡化了美 回复

    您的代码中有2个问题:

    • You have infinite loop since player is initialized to False before the loop and not changed inside of it (but maybe this is the desired behaviour).
    • The main problem that both the computer and the user choice are outside the loop and it causes them to be picked once and stay the same during all you game. If you want to change the picked values in each round, move those line inside the loop.