用python写一段人机猜拳游戏
时间: 2023-06-30 21:11:43 浏览: 105
python实现人机猜拳小游戏
好的,以下是一个简单的人机猜拳游戏的Python代码:
```python
import random
print("欢迎来到人机猜拳游戏!")
print("请出拳:1. 石头 2. 剪刀 3. 布")
while True:
player = int(input("请输入您的选择(1-3):"))
computer = random.randint(1, 3)
if player == 1:
print("您出了石头!")
elif player == 2:
print("您出了剪刀!")
elif player == 3:
print("您出了布!")
else:
print("输入错误,请重新出拳!")
continue
if computer == 1:
print("电脑出了石头!")
elif computer == 2:
print("电脑出了剪刀!")
elif computer == 3:
print("电脑出了布!")
if player == computer:
print("平局!")
elif player == 1 and computer == 2 or player == 2 and computer == 3 or player == 3 and computer == 1:
print("恭喜您获胜!")
else:
print("很遗憾,电脑获胜!")
choice = input("是否继续游戏?(y/n)")
if choice.lower() != 'y':
break
print("游戏结束!")
```
运行这段代码后,你可以和计算机进行多轮的猜拳游戏,直到你选择结束游戏为止。
阅读全文