python-5 if语句和石头剪刀布综合实验

#判断玩家胜利的条件
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("您输了")

if语句就是条件满足则执行,条件不满足则不执行,一个条件可能需要判断多次,如果if条件不满足,elif则继续判断是否满足,如果再不满足,就进入else,这两个都不满足则执行else里面语句。
if elif else 这三个模块只可以完成一个模块里的代码语句,按照优先级先后排序
电脑不能一直出布或者剪刀,我们需要用到random工具包,用import关键字导入该工具包,在代码最顶部的时候写导入,这样整篇代码都可以用,我们需要用到里面随机数生成的函数random.randint(1,3)
并且玩家的石头剪刀布.需要我们输入
player=int(input(“请输入您的石头剪刀布(1:石头 2:剪刀 3:布)”))
这样就可以实现简易版的石头剪刀布,这是全部代码

#导入随机工具包,导入工具包放在代码最前面
import random

player=int(input("请输入您的石头剪刀布(1:石头 2:剪刀 3:布)"))
computer=random.randint(1,3)
print("您的出拳为%d----电脑的出拳为%d"%(player,computer))
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("您输了")

python-5 if语句和石头剪刀布综合实验_第1张图片
python-5 if语句和石头剪刀布综合实验_第2张图片

你可能感兴趣的:(python)