我的主要功能是无限循环重复,但我不知道为什么

Jax*_*123 2 python class function

我正在运行一个播放石头,纸张,剪刀的程序。我已多次执行该代码,每次执行时,要求用户选择0,1,2的输入都会重复执行多次。每个游戏周期只应询问一次。谁能帮我弄清楚为什么会这样,如何解决?

import random

# Function: Display Menu
# Input: none
# Output: none
# displays the game rules to the user
def displayMenu():
    print("Welcome! Let's play rock, paper, scissors.")
    print("The rules of the game are:")
    print("\tRock smashes scissors")
    print("\tScissors cut paper")
    print("\tPaper covers rock")
    print("\tIf both the choices are the same, it's a tie")

# Function: Get Computer Choice
# Input: none
# Output: integer that is randomly chosen, a number between 0 to 2
def getComputerChoice():
    computerChoice = random.randrange(0,3)
    return computerChoice

# Function: Get Player Choice
# Input: none
# Output: integer that represents the choice
# Asks the user for their choice: 0 for rock, 1 for paper, or 2 for scissors
def getPlayerChoice():
    playerChoice = int(input("Please choose (0) for rock, (1) for paper or (2) for scissors"))
    return playerChoice

# Function: Play Round
# Input: two integers--one representing the computer's choice and the other representing the player's choice
# Output: integer (-1 if computer wins, 1 if player wins, 0 if there is a tie)
# This method contains the game logic so it stimulates the game and determines a winner
def playRound(getcomputerChoice, getplayerChoice):
    if getplayerChoice == 0 and getcomputerChoice == 2:
        return 1
    elif getcomputerChoice == 0 and getplayerChoice == 2:
        return -1
    elif getplayerChoice == 2 and getcomputerChoice == 1:
        return 1
    elif getcomputerChoice == 2 and getplayerChoice == 1:
        return -1
    elif getplayerChoice == 1 and getcomputerChoice == 0:
        return 1
    elif getcomputerChoice == 1 and getplayerChoice == 0:
        return 1
    else:
        return 0

# Function: Continue Game
# Input: none
# Output: boolean
# Ask the user is they want to continue (Y/N), and then return True or False accordingly
def continueGame():
    playAgain = input("Do you want to continue playing? Enter (y) for yes or (n) for no.")
    if playAgain.lower() == "y":
        return True
    elif playAgain.lower() == "n":
        return False

# Function: main
# Input: none
# Output: none
def main():
    displayMenu()
    getPlayerChoice()
    if getPlayerChoice() == 0:
        choicePlayer = "rock"
    elif getPlayerChoice() == 1:
        choicePlayer = "paper"
    elif getPlayerChoice() == 2:
        choicePlayer = "scissors"
    getComputerChoice()
    if getComputerChoice() == 0:
        choiceComputer = "rock"
    elif getComputerChoice() == 1:
        choiceComputer = "paper"
    elif getComputerChoice() == 2:
        choiceComputer = "scissors"
    print("You chose", choicePlayer + ".")
    print("The computer chose", choiceComputer + ".")
    playRound(getComputerChoice(), getPlayerChoice())
    continueGame()
    while continueGame() == True:
        displayMenu()
        getPlayerChoice()
        getComputerChoice()
        playRound(getComputerChoice(), getPlayerChoice())
        continueGame()

    playerCounter = 0
    computerCounter = 0
    tieCounter = 0

    while playRound(getPlayerChoice(), getPlayerChoice()) == -1:
        computerCounter += 1
    while playRound(getPlayerChoice(), getPlayerChoice()) == 1:
        playerCounter += 1
    while playRound(getPlayerChoice(), getPlayerChoice()) == 0:
        tieCounter += 1

    print()
    print("You won", playerCounter, "game(s).")
    print("The computer won", computerCounter, "game(s).")
    print("You tied with the computer", tieCounter, "time(s).")
    print()
    print("Thanks for playing!")

# Call Main
main()
Run Code Online (Sandbox Code Playgroud)

Mig*_*uel 5

这是因为当您这样做时:

    if getPlayerChoice() == 0:
    choicePlayer = "rock"
Run Code Online (Sandbox Code Playgroud)

您实际上是在再次调用该方法。

你应该做:

p_choice = getPlayerChoice()

if p_choice == 0:
...
Run Code Online (Sandbox Code Playgroud)