python猜拳游戏三局两胜
时间: 2024-01-29 12:10:36 浏览: 175
以下是一个实现Python猜拳游戏三局两胜的例子:
```python
import random
def play_game(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 get_computer_choice():
choices = ["石头", "剪刀", "布"]
return random.choice(choices)
player_score = 0
computer_score = 0
rounds = 0
while player_score < 2 and computer_score < 2:
rounds += 1
print(f"第{rounds}局开始")
player_choice = input("请输入你的选择(石头、剪刀、布):")
computer_choice = get_computer_choice()
result = play_game(player_choice, computer_choice)
if result == "平局":
print("这局是平局")
elif result == "玩家胜利":
print("你赢了这局")
player_score += 1
else:
print("电脑赢了这局")
computer_score += 1
print(f"你的选择:{player_choice},电脑的选择:{computer_choice}")
print(f"当前比分:玩家 {player_score} - 电脑 {computer_score}")
print()
if player_score > computer_score:
print("恭喜你,你赢得了比赛!")
else:
print("很遗憾,电脑赢得了比赛!")
```
阅读全文
相关推荐

















