小时候经常玩一个游戏,几个人把手背在后面,出石头,剪刀,布,现在身边没小伙伴了,就只能跟电脑斗智斗勇了,利用random的随机数来模拟随机出拳
random.randint(1,3) -- 返回1~3之间的整数
代码如下
# 猜拳游戏 import random # 获取玩家出的拳 player = int(input('玩家请出拳 石头(1)/剪刀(2)/布(3)')) # 输出玩家出的拳 if player == 1: print('玩家出的拳是:石头') elif player == 2: print('玩家出的拳是:剪刀') elif player == 3: print('玩家出的拳是:布') else: print('你出的啥玩意?') # 电脑出拳 computer = random.randint(1, 3) # 输出电脑出的拳 if computer == 1: print('电脑出的拳是:石头') elif computer == 2: print('电脑出的拳是:剪刀') elif computer == 3: print('电脑出的拳是:布') else: print('电脑坏了') # 判断胜负 if ((player == 1 and computer == 2) or (player == 2 and computer == 3) or (player == 3 and computer == 1)): print('垃圾电脑,敢跟我斗?') elif player == computer: print('垃圾电脑,敢跟我出一样的!') else: print('垃圾电脑,不讲武德,看我不砸了你!')