用python写个猜拳小游戏
时间: 2023-05-24 18:04:02 浏览: 115
A:
```python
import random
def play():
while True:
print('请选择:1.石头 2.剪刀 3.布')
player = int(input())
if player <= 0 or player >= 4:
print('输入有误,请重新输入')
continue
else:
break
computer = random.randint(1, 3)
print('电脑出了:', end='')
if computer == 1:
print('石头')
elif computer == 2:
print('剪刀')
else:
print('布')
if player == computer:
print('平局')
elif (player == 1 and computer == 2) or \
(player == 2 and computer == 3) or \
(player == 3 and computer == 1):
print('你赢了')
else:
print('你输了')
while True:
print('是否开始游戏?Y/N')
answer = input()
if answer == 'Y' or answer == 'y':
play()
elif answer == 'N' or answer == 'n':
break
else:
print('输入有误,请重新输入')
continue
```
运行结果示例:
```
是否开始游戏?Y/N
Y
请选择:1.石头 2.剪刀 3.布
1
电脑出了:布
你输了
是否开始游戏?Y/N
Y
请选择:1.石头 2.剪刀 3.布
2
电脑出了:布
你输了
是否开始游戏?Y/N
Y
请选择:1.石头 2.剪刀 3.布
3
电脑出了:剪刀
你赢了
是否开始游戏?Y/N
N
```
阅读全文