做一个石头剪刀布(三局两胜)的程序
时间: 2024-02-07 14:02:51 浏览: 78
好的,以下是一个简单的 Python 程序实现石头剪刀布游戏(三局两胜):
```python
import random
options = ['石头', '剪刀', '布']
win_map = {'石头': '剪刀', '剪刀': '布', '布': '石头'}
player_score = 0
computer_score = 0
while player_score < 2 and computer_score < 2:
computer_option = random.choice(options)
player_option = input('请出拳(石头、剪刀、布):')
if player_option not in options:
print('输入错误,请重新出拳!')
continue
print(f'你出了 {player_option},电脑出了 {computer_option}')
if player_option == computer_option:
print('平局!')
elif win_map[player_option] == computer_option:
print('你赢了!')
player_score += 1
else:
print('电脑赢了!')
computer_score += 1
if player_score > computer_score:
print('恭喜你,你赢得了比赛!')
else:
print('很遗憾,你输了比赛!')
```
这个程序会先生成一个选项列表,然后通过随机选择电脑出拳的选项。每次循环中,程序会要求玩家输入出拳选项,然后比较玩家和电脑的出拳,判断胜负,最后输出比赛结果。
阅读全文