3.3 猜拳游戏
更新于 2026年10月13日
版权声明
3.3 猜拳游戏

需求分析:
参与游戏的角色
1 玩家
手动出拳
2计算机
随机出拳
判断输赢:玩家获胜

平局
玩家出拳和计算机出拳相同(https://www.daowen.com)
计算机获胜
随机做法:
①导出random模块。
import 模块名
②使用random模块中的随机整数功能。
random.randint(开始,结束)
"""
提示: 0-石头, 1-剪刀, 2-布
1. 出拳
玩家输入出拳
计算机随机出拳
2. 判断输赢
玩家获胜
平局
计算机获胜
"""
# 导入random模块
import random
# 计算计算机出拳的随机数字
computer = random.randint(0, 2)
print(computer)
player = int(input('请出拳: 0-石头, 1-剪刀, 2-布: '))
# 玩家胜利 p0:c1 或 p1:c2 或 p2:c0
if ((player ==0) and (computer ==1) or (player ==1) and (computer ==2) or(player ==2) and (computer ==0)):
print('玩家获胜')
# 平局:玩家 == 计算机
elif player == computer:
print('平局')
else:
print('计算机获胜')
运行结果:
1
请出拳: 0-石头, 1-剪刀, 2-布:2
计算机获胜