python五局三胜
时间: 2023-12-28 17:25:43 浏览: 129
以下是一个简单的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 < 3 and computer_score < 3:
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 == "玩家胜利":
player_score += 1
print("玩家胜利")
else:
computer_score += 1
print("电脑胜利")
print(f"当前比分:玩家 {player_score} - 电脑 {computer_score}\n")
if player_score == 3:
print("玩家获胜!")
else:
print("电脑获胜!")
```
阅读全文