python写人与人猜拳代码
时间: 2023-04-06 10:01:14 浏览: 74
你好,我可以回答这个问题。以下是一个简单的 Python 代码,用于实现人与人猜拳游戏:
```python
import random
def get_player_choice():
choice = input("请出拳(石头/剪刀/布):")
while choice not in ["石头", "剪刀", "布"]:
choice = input("请重新出拳(石头/剪刀/布):")
return choice
def get_computer_choice():
choices = ["石头", "剪刀", "布"]
return random.choice(choices)
def get_winner(player_choice, computer_choice):
if player_choice == computer_choice:
return "平局"
elif (player_choice == "石头" and computer_choice == "剪刀") or \
(player_choice == "剪刀" and computer_choice == "布") or \
(player_choice == "布" and computer_choice == "石头"):
return "玩家胜利"
else:
return "电脑胜利"
def play_game():
print("欢迎来到人与人猜拳游戏!")
while True:
player_choice = get_player_choice()
computer_choice = get_computer_choice()
print("玩家出拳:", player_choice)
print("电脑出拳:", computer_choice)
winner = get_winner(player_choice, computer_choice)
print("本局结果:", winner)
play_again = input("是否再来一局?(是/否):")
if play_again != "是":
break
print("游戏结束,谢谢参与!")
play_game()
```
希望能帮到你!
阅读全文