我正在尝试制作带有功能的石头剪刀布-铅笔射击游戏,但它不起作用。似乎它无法进入比较函数(名为findwinner())并返回结果。
我看似在网上寻求帮助,但找不到任何东西。我尝试以许多不同的方式分配变量,但是没有运气。
python
#Rock-Paper-Scissors-Fire-Pencil Game
#Game:
#The player must choose his weapon and 'fight' against the computer following #the rules above.
#The player can choose between the following: Rock, Scissors, Fire, Pencil and #Paper.
#Rules: The rock beats the scissors, the scissors beat the fire, the fire #beats the pencil, the pencil beats the paper and the paper beats the rock
#Player1 is the human player
#Player2 is the computer
import random
#The game welcomes the player.
print("Wellcome to the Rock-Paper-Scissors-Fire-Pencil game!")
#The player can play as many times as he/she wants until he/she type anything #else except yes in the "Do you want to play again?" question.
def getuser():
choices = ["rock", "scissors", "fire", "pencil", "paper"]
#The computer chooses it's 'weapon'.
player2 = choices[random.randint(0,4)]
player1 = None
while player1 not in choices:
#The player must choose his/her 'weapon'
player1 = input("\n PLAYER 1 - Please make a choice (rock/paper/scissors/fire/pencil):")
print (f"\nPlayer1 choose {player1}")
print (f"\nPlayer2 choose {player2}")
return ()
def findwinner():
winner = None
p1 = getuser()[1]
p2 = getuser()[2]
#The game keeps how many times the player1 has won, lost or tied with the computer in the variables bellow.
wins = 0
losses = 0
ties = 0
#The game compares the choises of the players and announces if the human player won, lost or tied with the computer.
#Then it adds the win, the lost or the tie to the summary of wins, losses or ties.
if p1 != p2:
if p1 == "rock":
if p2 == "scissors" or p2 == "pencil" or p2 == "fire":
print("\n Player1 wins this round!")
winner = p1
wins += 1
else:
print("\n Player2 wins this round!")
winner = p2
losses += 1
if p1 == "paper":
if p2 == "rock":
print("\n Player1 wins this round!")
winner = p1
wins += 1
else:
print("\n Player2 wins this round!")
winner = p2
losses += 1
if p1 == "scissors":
if p2 == "paper" or p2 == "pencil":
print("\n Player1 wins this round!")
winner = p1
wins += 1
else :
print("\n Player2 wins this round!")
winner = p2
losses += 1
if p1 == "pencil":
if p2 == "paper":
print("\n Player1 wins this round!")
winner = p1
wins += 1
else:
print("\n Player2 wins this round!")
winner = p2
losses += 1
if p1 == "fire":
if p2 == "paper" or p2 == "scissors" or p2 == "pencil":
print("\n Player1 wins this round!")
winner = p1
wins += 1
else:
print("\n Player2 wins this round!")
winner = p2
losses += 1
else:
print("\n The round is a tie!")
winner = None
ties += 1
print (f"\nThe winner of this round is {winner}")
return (winner, wins, losses, ties)
play_again = "yes"
while play_again == "yes":
#The game shows the choises of the players.
player1 = getuser()[1]
player2 = getuser()[2]
findwinner(player1, player2)
#The game askes the player if he/she wants to play again.
play_again = input("\nDo you want to play again?:")
else:
#If the player doesn't want to play again, the game prints the results of all the rounds of the game and closes.
print(f"The player1 won {findwinner()[2]} times")
print(f"The player1 lost {findwinner()[3]} times")
print(f"The player1 tied {findwinner()[4]} times")
print("\n Good Bye")
答案 0 :(得分:0)
您遇到了两个问题:
getuser()仅显示选择但不返回选择,要解决此问题,只需将返回值更改为player1,player2
findwinner()根据签名没有获得任何参数,您必须将签名更改为findwinner(p1,p2)
如果您正在findwinner()中通过参数接收玩家的选择,则不必再次调用getuser()。
每次调用findwinner()时,都会重置赢,输和平局计数器,您必须将它们声明为函数外部的全局变量,然后在函数内部进行更新。
修复这些问题后,您将获得以下结果:
import random
#The game keeps how many times the player1 has won, lost or tied with the computer in the variables bellow.
# 4 FIX) declare counters as globals
wins = 0
losses = 0
ties = 0
#The game welcomes the player.
print("Welcome to the Rock-Paper-Scissors-Fire-Pencil game!")
#The player can play as many times as he/she wants until he/she type anything
#else except yes in the "Do you want to play again?" question.
def getuser():
choices = ["rock", "scissors", "fire", "pencil", "paper"]
#The computer chooses it's 'weapon'.
player2 = choices[random.randint(0,4)]
player1 = None
while player1 not in choices:
#The player must choose his/her 'weapon'
player1 = input("\n PLAYER 1 - Please make a choice (rock/paper/scissors/fire/pencil):")
print (f"\nPlayer1 choose {player1}")
print (f"\nPlayer2 choose {player2}")
return player1, player2 # 1 FIX) return players choice
# 2 FIX) add players choice as parameters
def findwinner(p1, p2):
winner = None
# 3 FIX) do not call getuser since you have the player choices as arguments
# 4 FIX) use them as globals in the function scope
global wins, losses, ties
#The game compares the choises of the players and announces if the human player won, lost or tied with the computer.
#Then it adds the win, the lost or the tie to the summary of wins, losses or ties.
if p1 != p2:
if p1 == "rock":
if p2 == "scissors" or p2 == "pencil" or p2 == "fire":
print("\n Player1 wins this round!")
winner = p1
wins += 1
else:
print("\n Player2 wins this round!")
winner = p2
losses += 1
if p1 == "paper":
if p2 == "rock":
print("\n Player1 wins this round!")
winner = p1
wins += 1
else:
print("\n Player2 wins this round!")
winner = p2
losses += 1
if p1 == "scissors":
if p2 == "paper" or p2 == "pencil":
print("\n Player1 wins this round!")
winner = p1
wins += 1
else :
print("\n Player2 wins this round!")
winner = p2
losses += 1
if p1 == "pencil":
if p2 == "paper":
print("\n Player1 wins this round!")
winner = p1
wins += 1
else:
print("\n Player2 wins this round!")
winner = p2
losses += 1
if p1 == "fire":
if p2 == "paper" or p2 == "scissors" or p2 == "pencil":
print("\n Player1 wins this round!")
winner = p1
wins += 1
else:
print("\n Player2 wins this round!")
winner = p2
losses += 1
else:
print("\n The round is a tie!")
winner = None
ties += 1
print (f"\nThe winner of this round is {winner}")
return winner # 4 FIX) no need to return the counters anymore
play_again = "yes"
while play_again == "yes":
#The game shows the choises of the players.
player1, player2 = getuser() # 1 FIX) save player choices
findwinner(player1, player2)
#The game askes the player if he/she wants to play again.
play_again = input("\nDo you want to play again?:")
else:
#If the player doesn't want to play again, the game prints the results of all the rounds of the game and closes.
# 4 FIX) Use the global variables
print(f"The player1 won {wins} times")
print(f"The player1 lost {losses} times")
print(f"The player1 tied {ties} times")
print("\n Good Bye")
我可以告诉您在理解函数返回值时遇到问题。每次执行findwinner()时,您都在调用函数AGAIN,以仅运行一次并将其保存结果分配给变量。就像对getuser()一样:
player1, player2 = getuser()