python实现人机猜拳游戏
时间: 2023-07-17 20:43:11 浏览: 109
python实现猜拳游戏
好的,以下是Python实现:
```python
import random
print("欢迎来到人机猜拳游戏!")
print("请出拳:1.石头 2.剪刀 3.布")
while True:
player = int(input("请出拳:"))
if player == 1:
print("玩家出拳:石头")
elif player == 2:
print("玩家出拳:剪刀")
elif player == 3:
print("玩家出拳:布")
else:
print("输入错误,请重新输入!")
continue
computer = random.randint(1, 3)
if computer == 1:
print("电脑出拳:石头")
elif computer == 2:
print("电脑出拳:剪刀")
else:
print("电脑出拳:布")
if (player == 1 and computer == 2) or (player == 2 and computer == 3) or (player == 3 and computer == 1):
print("玩家获胜!")
elif player == computer:
print("平局!")
else:
print("电脑获胜!")
play_again = input("再玩一局吗?(y/n)")
if play_again == "n":
print("游戏结束,再见!")
break
```
程序会进入一个循环,每次先让玩家输入出拳的数字,分别代表石头、剪刀和布,然后程序会随机生成电脑出拳的结果。接着,程序会判断出拳的结果,根据规则判断胜负,并输出相应的信息。最后询问玩家是否再玩一局,如果输入"n"则退出游戏,否则继续进行游戏。
阅读全文