python编写猜拳程序
时间: 2023-08-08 09:08:13 浏览: 151
python实现猜拳游戏
好的,以下是一个简单的 Python 猜拳游戏程序:
```python
import random
print("欢迎来到猜拳游戏!")
while True:
player = input("请出拳(1-石头,2-剪刀,3-布):")
while player not in ["1", "2", "3"]:
player = input("输入有误,请重新出拳(1-石头,2-剪刀,3-布):")
player = int(player)
computer = random.randint(1, 3)
print("你出了%s,电脑出了%s。" % (player, computer))
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("很遗憾,你输了。")
play_again = input("再玩一局?(是/否):")
play_again = play_again.lower()
while play_again not in ["是", "否"]:
play_again = input("输入有误,请重新输入(是/否):")
play_again = play_again.lower()
if play_again == "否":
break
print("游戏结束,谢谢参与!")
```
程序会不断循环,直到玩家选择退出游戏。玩家和电脑各出一种手势,程序会判断胜负并输出结果。玩家可以选择再玩一局或退出游戏。
阅读全文