python 猜丁壳完成剪刀石头布游戏代码
时间: 2023-08-28 17:04:55 浏览: 54
好的,以下是 Python 猜拳游戏代码:
```python
import random
print("欢迎来到猜拳游戏!")
while True:
# 用户出拳
user_choice = input("请出拳(剪刀/石头/布):")
while user_choice not in ["剪刀", "石头", "布"]:
user_choice = input("输入有误,请重新出拳(剪刀/石头/布):")
# 电脑出拳
computer_choice = random.choice(["剪刀", "石头", "布"])
print("电脑出拳:", computer_choice)
# 判断胜负
if user_choice == computer_choice:
print("平局!")
elif (user_choice == "剪刀" and computer_choice == "布") or \
(user_choice == "石头" and computer_choice == "剪刀") or \
(user_choice == "布" and computer_choice == "石头"):
print("恭喜你,你赢了!")
else:
print("很遗憾,你输了!")
# 询问是否继续
choice = input("是否继续(是/否):")
while choice not in ["是", "否"]:
choice = input("输入有误,请重新选择(是/否):")
if choice == "否":
break
print("游戏结束,谢谢参与!")
```
使用方法:运行程序后,按照提示输入剪刀、石头或布,程序会自动随机出拳并判断胜负。最后询问是否继续,输入“是”则继续,输入“否”则结束游戏。