python人机猜拳
时间: 2023-11-19 07:57:48 浏览: 384
python实现人机猜拳小游戏
以下是一个简单的Python人机猜拳游戏的例子:
```python
import random
player = int(input('请输入猜拳数字0-石头,1-剪刀,2-布:'))
computer = random.randint(0,2)
if player == 0 and computer == 1 or player == 1 and computer == 2 or player == 2 and computer == 0:
print('玩家获胜,哈哈哈')
elif player == 0 and computer == 0 or player == 1 and computer == 1 or player == 2 and computer == 2:
print('平局,别走!再来一局!')
else:
print('电脑获胜,呜呜呜')
```
在这个例子中,我们使用了Python内置的`random`模块来生成电脑的随机选择。玩家需要输入一个数字来代表他们的选择,然后程序会根据玩家和电脑的选择来判断胜负并输出结果。
阅读全文