编写程序孩子与电脑玩石头剪刀布游戏,每次孩子随机出拳。1---代表石头,2---代表剪刀,3---代表布。程序提示玩家出拳,并判断电脑赢还是玩家赢。
时间: 2024-05-07 14:21:27 浏览: 14
好的,以下是Python代码实现:
```python
import random
print("欢迎来玩石头剪刀布游戏!")
print("请出拳:1 - 石头,2 - 剪刀,3 - 布")
while True:
player = int(input("请出拳:"))
computer = random.randint(1, 3)
if player == 1:
print("你出了石头")
elif player == 2:
print("你出了剪刀")
elif player == 3:
print("你出了布")
if (player == 1 and computer == 2) or (player == 2 and computer == 3) or (player == 3 and computer == 1):
print("电脑出了", computer, ",你赢了!")
elif player == computer:
print("电脑出了", computer, ",平局!")
else:
print("电脑出了", computer, ",你输了!")
play_again = input("是否再玩一局?(Y/N)")
if play_again.lower() == "n":
break
print("游戏结束,欢迎下次再来!")
```
程序首先输出欢迎信息,并且提示玩家出拳。然后进入一个无限循环,每次循环中,程序随机生成电脑出的拳头,同时获取玩家输入的拳头。接着程序判断电脑和玩家出拳的胜负关系,输出胜负结果。最后询问玩家是否再玩一局,如果玩家不想再玩就退出循环,结束游戏。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)