一、if判断语句
if语句是用来进行判断的,其使用格式如下:

 if 要判断的条件:
        条件成立时,要做的事情
    else 这里写代码片否者
       要做的事情

二、框图

三、参考代码:

 chePiao = 1 
    if chePiao == 1:
        print("hell")
    else:
        print("hell word")

四、elif
if xxx1:
事情1
elif xxx2:
事情2
elif xxx3:

参考代码:

    score = 77

    if score>=90 and score<=100:
        print('本次考试,等级为A')
    elif score>=80 and score<90:
        print('本次考试,等级为B')
    elif score>=70 and score<80:
        print('本次考试,等级为C')
    elif score>=60 and score<70:
        print('本次考试,等级为D')
    elif score>=0 and score<60:
        print('本次考试,等级为E') 

五、与else一起使用
参考代码:

num = 3
if num>=5:
    print('good')
elif ((num<5) and (num>=3)):
    print('secondary')
else:
    print('wanting')

六、if的嵌套

 chePiao = 1     # 用1代表有车票,0代表没有车票
    daoLenght = 9     # 刀子的长度,单位为cm

    if chePiao == 1:
        print("有车票,可以进站")
        if daoLenght < 10:
            print("通过安检")
            print("终于可以见到Ta了,美滋滋~~~")
        else:
            print("没有通过安检")
            print("刀子的长度超过规定,等待警察处理...")
    else:
        print("没有车票,不能进站")
        print("亲爱的,那就下次见了,一票难求啊~~~~(>_<)~~~~")

更多推荐

python中if的用法