用python写一个猜拳游戏代码
时间: 2023-04-11 18:01:15 浏览: 160
好的,我可以回答这个问题。以下是一个简单的猜拳游戏的Python代码:
```python
import random
print("欢迎来到猜拳游戏!")
print("请出拳:1.石头 2.剪刀 3.布")
while True:
player = int(input("请输入你的选择(1/2/3):"))
computer = random.randint(1, 3)
if player == 1:
if computer == 1:
print("平局!")
elif computer == 2:
print("你赢了!")
else:
print("你输了!")
elif player == 2:
if computer == 1:
print("你输了!")
elif computer == 2:
print("平局!")
else:
print("你赢了!")
elif player == 3:
if computer == 1:
print("你赢了!")
elif computer == 2:
print("你输了!")
else:
print("平局!")
else:
print("请输入有效的选择!")
play_again = input("是否再来一局?(y/n)")
if play_again.lower() != "y":
break
print("游戏结束,谢谢参与!")
```
希望这个代码能够帮到你!
阅读全文