利用python编写一个石头剪刀布的游戏
时间: 2024-05-15 20:18:50 浏览: 106
好的,我可以为您编写一个石头剪刀布的游戏。以下是Python代码示例:
```python
import random
options = ["石头", "剪刀", "布"]
while True:
player_choice = input("请输入您的选择(石头/剪刀/布):")
if player_choice not in options:
print("输入错误,请重新输入")
continue
computer_choice = random.choice(options)
print("您的选择:", player_choice)
print("电脑的选择:", computer_choice)
if player_choice == computer_choice:
print("平局!")
elif (player_choice == "石头" and computer_choice == "剪刀") or \
(player_choice == "剪刀" and computer_choice == "布") or \
(player_choice == "布" and computer_choice == "石头"):
print("恭喜你,你赢了!")
else:
print("很遗憾,你输了。")
play_again = input("是否再玩一局?(是/否)")
if play_again.lower() != "是":
break
```
这个游戏会随机生成电脑的选择,并与玩家的选择进行比较,最后给出胜负结果。玩家可以选择是否再玩一局。
阅读全文