#python #function #printing
#python #функция #печать
Вопрос:
У меня есть программа «камень, ножницы, бумага», которая работает так, как ей нужно, с одной незначительной ошибкой. Предполагается, что в выводе указывается, что выбирает игрок, что выбирает компьютер, а затем, например, кто выигрывает. Если проигрыватель выбирает rock, а компьютер выбирает paper, вывод должен гласить:
Вы выбрали rock. Компьютер выбрал ножницы. Камень разбивает ножницы. Игрок выигрывает!
Я думал, что добавил последнее предложение в нужную функцию, но когда я запускаю программу, она выводится не так, как ожидалось.
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()
Комментарии:
1. Покажите нам наблюдаемый и желаемый результат.
Ответ №1:
Вы передаете аргументы в неправильном порядке. Ваша сигнатура функции ожидает computerChoice
в качестве своего первого аргумента:
def playRound(computerChoice, playerChoice):
Поэтому измените следующую строку:
result = playRound(p_choice, c_choice)
Для
result = playRound(c_choice, p_choice)