我的代码没有在石头剪刀布游戏中比较两个值以产生正确的结果。字典或我比较播放器和计算机选择的方式似乎有问题。例如,有时玩家选择纸,而计算机选择石头,然后打平。
这是我在python 3中的代码
#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 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 welcomes the player.
print("Welcome to the Rock-Paper-Scissors-Fire-Pencil game!")
#The hierarchy of which weapon beats another is added in a dictionary and
#the choices of the player in a table.
rules = {'rock': {'scissors', 'pencil', 'fire'}, 'scissors': {'paper',
'pencil'}, 'fire': {'pencil', 'paper', 'scissors'}, 'pencil': 'paper',
'paper': 'rock'}
choices = ['rock', 'scissors', 'fire', 'pencil', 'paper']
#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.
play_again = "yes"
while play_again == "yes":
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):")
#The computer chooses it's 'weapon'.
random_number = random.randint(0,4)
player2 = choises[random_number]
#The game shows the choices of the players.
print (f"\nPlayer 1 has chosen {player1}")
print (f"\nPlayer 2 has chosen {player2}")
#The game compares the choices 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 rules[player2] == player1:
print('Player1 wins the round!')
wins += 1
elif rules[player1] == player2:
print('Player2 wins the round!')
losses += 1
else:
print('The round is a tie!')
ties += 1
#The game asks the player if he/she wants to play again.
play_again = input("\nDo you want to play again?:")
#If the player doesn't want to play again, the game prints the results of
#all the rounds of the game and closes.
else:
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")
答案 0 :(得分:2)
夫妇:
1)使用代码缩进注释。
2)rules[player1]
返回一个set
,为什么set
与一个单独的str
相匹配?
3)random
可以为您选择一个随机元素。
请参阅下面的代码。我尝试将##########
放在所有更改之前。
import random
wins, losses, ties = 0, 0, 0
print("Wellcome to the Rock-Paper-Scissors-Fire-Pencil game!")
rules = {'rock': {'scissors', 'pencil', 'fire'}, 'scissors': {'paper',
'pencil'}, 'fire': {'pencil', 'paper', 'scissors'}, 'pencil': 'paper',
'paper': 'rock'}
############ This should be a set not a list (faster look up time)
choices = {'rock', 'scissors', 'fire', 'pencil', 'paper'}
play_again = "yes"
while play_again == "yes":
player1_choice = None
while player1_choice not in choices:
player1_choice = input("\n PLAYER 1 - Please make a choice (rock/paper/scissors/fire/pencil):")
######### Random can choose for you.
player2_choice = random.choice(list(choices))
print (f"\nPlayer 1 has choosen {player1_choice}")
print (f"\nPlayer 2 has choosen {player2_choice}")
########## Don't use ==, use `in` for sets.
if player1_choice in rules[player2_choice]:
print('Player 2 wins the round!')
wins += 1
elif player2_choice in rules[player1_choice]:
print('Player 1 wins the round!')
losses += 1
else:
print('The round is a tie!')
ties += 1
play_again = input("\nDo you want to play again?:")
########## Why was there an `else` here?
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")