写一个冒泡排序的python代码
时间: 2023-08-30 12:07:29 浏览: 96
以下是用Python写的石头剪刀布游戏代码,三局两胜:
```python
import random
# 定义游戏规则
rules = {'rock': 'scissors', 'scissors': 'paper', 'paper': 'rock'}
# 定义初始分数
player_score =
computer_score =
# 进行三局比赛
for i in range(3):
print('第%d局比赛开始' % (i+1))
# 玩家出拳
player_choice = input('请出拳(rock/scissors/paper):')
while player_choice not in rules.keys():
player_choice = input('输入有误,请重新出拳(rock/scissors/paper):')
# 电脑出拳
computer_choice = random.choice(list(rules.keys()))
print('电脑出拳:%s' % computer_choice)
# 判断胜负
if rules[player_choice] == computer_choice:
print('你赢了!')
player_score += 1
elif player_choice == computer_choice:
print('平局!')
else:
print('你输了!')
computer_score += 1
# 判断是否已经有两局胜利者
if player_score == 2:
print('你已经获得两局胜利,游戏结束!')
break
elif computer_score == 2:
print('电脑已经获得两局胜利,游戏结束!')
break
# 输出最终结果
print('最终比分:你%d:%d电脑' % (player_score, computer_score))
```
运行代码后,程序会提示玩家输入出拳的选项(rock/scissors/paper),然后随机生成电脑出拳的选项,并根据游戏规则判断胜负。如果玩家或电脑获得两局胜利,则游戏结束,输出最终比分。
阅读全文