Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 未正确调用较大的主函数中的Print语句,并且未显示预期的输出_Python_Function_Printing - Fatal编程技术网

Python 未正确调用较大的主函数中的Print语句,并且未显示预期的输出

Python 未正确调用较大的主函数中的Print语句,并且未显示预期的输出,python,function,printing,Python,Function,Printing,我有一个石头,布,剪刀程序,它的工作方式需要一个小错误。输出应该说明玩家选择什么,计算机选择什么,然后是谁赢。如果玩家选择摇滚,电脑选择纸张,输出应为: 你选择了摇滚。电脑选择了剪刀。石头砸剪刀。玩家赢了 我以为我在正确的函数中添加了最后一句话,但当我运行程序时,它并没有按预期打印出来 import random # Function: Display Menu # Input: none # Output: none # displays the game rules to the user

我有一个石头,布,剪刀程序,它的工作方式需要一个小错误。输出应该说明玩家选择什么,计算机选择什么,然后是谁赢。如果玩家选择摇滚,电脑选择纸张,输出应为:

你选择了摇滚。电脑选择了剪刀。石头砸剪刀。玩家赢了

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

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

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

def playRound(computerChoice, playerChoice):
因此,请更改以下行:

result =  playRound(p_choice, c_choice)


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

def playRound(computerChoice, playerChoice):
因此,请更改以下行:

result =  playRound(p_choice, c_choice)


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