【问题标题】:Print statement within a larger main function is not being called properly and expected output is not showing up未正确调用较大主函数中的打印语句,并且未显示预期的输出
【发布时间】:2019-03-04 04:37:14
【问题描述】:

我有一个石头剪刀布程序,它可以按照需要的方式运行,但有一个小错误。输出应该说明玩家选择了什么,计算机选择了什么,然后谁赢了。如果玩家选择石头,电脑选择纸,输出应该是:

你选择了摇滚。电脑选择了剪刀。石头打碎剪刀。玩家获胜!

我以为我在正确的函数中添加了最后一句,但是当我运行程序时,它并没有按预期打印出来。

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:
        print("Rock smashes scissors. Player wins!")
        return 1
    elif computerChoice == 0 and playerChoice == 2:
        print("Rock smashes scissors. Computer wins!")
        return -1
    elif playerChoice == 2 and computerChoice == 1:
        print("Scissors cut paper. Player wins!")
        return 1
    elif computerChoice == 2 and playerChoice == 1:
        print("Scissors cut paper. Computer wins!")
        return -1
    elif playerChoice == 1 and computerChoice == 0:
        print("Paper covers rock. Player wins!")
        return 1
    elif computerChoice == 1 and playerChoice == 0:
        print("Paper covers rock. Computer wins!")
        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()
    next_game = True

    while next_game:
        p_choice = getPlayerChoice()
        if p_choice == 0:
            choicePlayer = "rock"
        elif p_choice == 1:
            choicePlayer = "paper"
        elif p_choice == 2:
            choicePlayer = "scissors"
        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 + ".")


        result = playRound(p_choice, c_choice)
        if result == -1:
            computerCounter += 1
        elif result == 0:
            tieCounter += 1
        else:
            playerCounter += 1

        next_game = continueGame()

    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()

【问题讨论】:

  • 向我们展示观察到的和期望的输出。

标签: python function printing


【解决方案1】:

您以错误的顺序传递参数。您的函数签名需要 computerChoice 作为其第一个参数:

def playRound(computerChoice, playerChoice):

因此更改以下行:

result =  playRound(p_choice, c_choice)

result =  playRound(c_choice, p_choice)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-11
    • 1970-01-01
    • 2020-12-11
    • 1970-01-01
    • 2014-09-19
    • 1970-01-01
    • 2017-04-04
    • 1970-01-01
    相关资源
    最近更新 更多