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

Python 我的主要功能是在无限循环中重复,我不知道为什么,python,function,class,Python,Function,Class,我正在运行一个玩石头、布、剪刀的程序。我已经多次执行代码,每次执行时,要求用户选择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 pl

我正在运行一个玩石头、布、剪刀的程序。我已经多次执行代码,每次执行时,要求用户选择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()

这是因为当你这样做的时候:

    if getPlayerChoice() == 0:
    choicePlayer = "rock"
实际上,您正在再次调用该方法

你应该做:

p_choice = getPlayerChoice()

if p_choice == 0:
...