猜拳游戏代码python
时间: 2023-10-12 20:56:24 浏览: 149
猜拳游戏的代码可以有多种实现方式。引用\[1\]和引用\[2\]提供了两种不同的实现方法。
方法一的代码如下:
```python
import random
d = {0:'石头',1:'剪刀',2:'布'}#创建字典
'''猜拳函数'''
def guess_game(player):
computer = random.randint(0, 2)#电脑随机产生0-2
if (player == 0 and computer == 1) or (player == 1 and computer == 2) or (player == 2 and computer == 0):
print('电脑出的是%s,玩家出的是%s。**玩家赢啦**' % (d\[computer\], d\[player\]))
elif player == computer:
print('电脑出的是%s,玩家出的是%s,**平局**' % (d\[computer\], d\[player\]))
else:
print('电脑出的是%s,玩家出的是%s,**玩家输了**' % (d\[computer\], d\[player\]))
print('\n')
while True:
print('猜拳游戏:0代表石头,1代表剪刀,2代表布。e退出游戏')
player = input('请出拳:0 ,1,2:\n')
if (player == 'e' or player == '0' or player == '1' or player == '2' ):
if player == 'e':
break
player = int(player)
guess_game(player)
else:
print('请输入0-2猜拳,或者输入e退出游戏')
print('\n')
```
方法二的代码如下:
```python
import random
you = int(input("请输入剪刀0,石头1,布2:"))
cp = random.randint(0,2)##生成范围是0到2的随机数
if(you > 2 or you < 0):
print("没有这种出法哦")##玩家不能输入0到2范围以外的数字
else:
print("电脑出的是 %d\n " % cp)
if(you == 0):
if(cp == 0):
print("平局,请再出一次")
elif(cp == 1):
print("你输了")
else:
print("你赢了")
if(you == 1):
if(cp == 1):
print("平局,请再出一次")
elif(cp == 2):
print("你输了")
else:
print("你赢了")
if(you == 2):
if(cp == 2):
print("平局,请再出一次")
elif(cp == 0):
print("你输了")
else:
print("你赢了")
```
这两种方法都可以实现猜拳游戏的功能,根据个人喜好和需求选择其中一种即可。
#### 引用[.reference_title]
- *1* [猜拳小游戏编程(python)](https://blog.csdn.net/xuekunshan_/article/details/80447189)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* *3* [用python做一个简单的猜拳游戏](https://blog.csdn.net/weixin_45761932/article/details/108611766)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文