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