Search code examples
pythonfunctionclassprogram-entry-point

Inability to execute the call function within my main statement for rock, paper, scissors


Before anyone marks this question as a duplicate of anyone else's relating to this type of program, know that I searched and read the answered questions on this topic and was unable to find anything that suits my needs.

In my program for rock, paper, scissors I am asked to figure out if the computer or the player wins, or if they tie. We are supposed to store computer win as -1, player win as 1, and tie as 0. I thought I wrote this function correctly and called it properly in my main function, however, when I run my code it skips right over my runGame function and instead skips to an infinite loop asking the player to input their choice. I do not know why this is happening. I should note that within my main function, we are supposed to keep a counter to see how many wins the computer has and the player has, and how many times they tie. I also am having difficulty getting this to execute.

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(computerChoice, playerChoice):
    if playerChoice == 0 and computerChoice == 2:
        return 1
    elif computerChoice == 0 and playerChoice == 2:
        return -1
    elif playerChoice == 2 and computerChoice == 1:
        return 1
    elif computerChoice == 2 and playerChoice == 1:
        return -1
    elif playerChoice == 1 and computerChoice == 0:
        return 1
    elif computerChoice == 1 and playerChoice == 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():
    playerCounter = 0
    computerCounter = 0
    tieCounter = 0

    displayMenu()
    p_choice = getPlayerChoice()
    if p_choice == 0:
        choicePlayer = "rock"
    elif p_choice == 1:
        choicePlayer = "paper"
    elif p_choice == 2:
        choicePlayer = "scissors"
    getComputerChoice()
    c_choice = getComputerChoice()
    if c_choice == 0:
        choiceComputer = "rock"
    elif c_choice == 1:
        choiceComputer = "paper"
    elif c_choice == 2:
        choiceComputer = "scissors"
    print("You chose", choicePlayer + ".")
    print("The computer chose", choiceComputer + ".")
    playRound(getComputerChoice(), getPlayerChoice())
    while playRound(c_choice, p_choice) == -1:
        computerCounter += 1
    while playRound(getPlayerChoice(), getPlayerChoice()) == 1:
        playerCounter += 1
    while playRound(getPlayerChoice(), getPlayerChoice()) == 0:
        tieCounter += 1
    continueGame()
    while continueGame() == True:
        displayMenu()
        getPlayerChoice()
        getComputerChoice()
        playRound(getComputerChoice(), getPlayerChoice())
        continueGame()
    while continueGame() == False:
        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()

Solution

  • You don't have a "runGame" method, I believe you are referring to playRound. In that case, once again, in this line:

     playRound(getComputerChoice(), getPlayerChoice())
    

    You are calling the getComputerChoice() and getPlayerChoice() methods again, and this is not what you want. Because of that it is asking you for the input again. You should do:

    playRound(c_choice, p_choice)