python剪刀石头布游戏
时间: 2023-08-24 22:07:06 浏览: 174
剪刀石头布
好的,以下是一个简单的Python剪刀石头布游戏代码:
```python
import random
options = ["剪刀", "石头", "布"]
while True:
player = input("剪刀、石头、布,你出什么?(退出请输q)")
if player == "q":
break
if player not in options:
print("无效的输入,请重新输入!")
continue
computer = random.choice(options)
print("电脑出了:", computer)
if player == computer:
print("平局!")
elif (player == "剪刀" and computer == "布") or \
(player == "石头" and computer == "剪刀") or \
(player == "布" and computer == "石头"):
print("你赢了!")
else:
print("你输了!")
```
运行代码后,用户可以输入剪刀、石头或布,程序会随机生成电脑的出招,并判断胜负。如果用户输入了 `q`,则游戏退出。
阅读全文