python写一个猜拳游戏代码
时间: 2024-02-18 16:56:29 浏览: 138
python实现人机猜拳小游戏
5星 · 资源好评率100%
以下是一个简单的Python猜拳游戏代码,满足题目要求:
```python
import random
# 定义猜拳规则
rules = {1: '剪刀', 2: '石头', 3: '布'}
# 定义电脑对手
computers = {'电脑1': 0, '电脑2': 0, '电脑3': 0}
# 定义玩家信息
player_name = input('请输入您的名字:')
player_score = 0
# 定义游戏局数
rounds = int(input('请输入游戏局数:'))
# 开始游戏
for i in range(rounds):
print('第%d局开始:' % (i+1))
print('请选择您的出拳手势:')
for key, value in rules.items():
print(key, value)
player_choice = int(input())
computer_choice = random.randint(1, 3)
print('电脑出拳手势为:%s' % rules[computer_choice])
if player_choice == computer_choice:
print('本局平局')
elif (player_choice == 1 and computer_choice == 3) or (player_choice == 2 and computer_choice == 1) or (player_choice == 3 and computer_choice == 2):
print('您赢了本局!')
player_score += 1
else:
print('电脑赢了本局!')
for key in computers.keys():
if computers[key] == computer_choice:
print('本局电脑为%s' % key)
computers[key] += 1
break
# 游戏结束,输出结果
print('游戏结束!%s的得分为%d分' % (player_name, player_score))
print('电脑得分情况为:')
for key, value in computers.items():
print('%s得分:%d分' % (key, value))
```
阅读全文