zoukankan      html  css  js  c++  java
  • 🍖流程控制之if判断

    引入

    • if判断是为了让计算机像人一样具有判断能力
    • 代码块表示同一级别缩进的所有代码,python默认缩进是四的字节
    • 伪代码可以理解成是逻辑代码 / 代码模型

    一.if 判断完整语法

    伪代码展示
    if 条件1:
        代码1
        代码2
        代码3
    elif 条件2:
        代码1
        代码2
        代码3
    elif 条件3:
        代码1
        代码2
        代码3
    ...
    else:
        代码1
        代码2
        代码3
    

    示例

    上班
    today=input('>>: ')
    if today == 'Monday':
        print('上班')
    elif today == 'Tuesday':
        print('上班')
    elif today == 'Wednesday':
        print('上班')
    elif today == 'Thursday':
        print('上班')
    elif today == 'Friday':
        print('上班')
    elif today == 'Saturday' or today == 'Sunday':
        print('出去浪')
    
    else:
        print('''必须输入其中一种:
        Monday
        Tuesday
        Wednesday
        Thursday
        Friday
        Saturday
        Sunday
        ''')
    

    二.if 单分支

    伪代码展示
    if 条件1:
        代码1
        代码2
        代码3
    

    示例

    相亲
    song='man'
    age=27
    is_verygood=True
    is_yes=False
    if song == 'man' and (age > 18 and age < 26 or is_verygood):
        print('结婚')
    

    三.if...else 双分支

    伪代码展示
    if 条件1:
        代码1
        代码2
        代码3
    else:
        代码1
        代码2
        代码3
    

    示例

    结婚
    song='man'
    age=27
    is_verygood=True
    if song == 'man' and (age > 18 and age < 26 or is_verygood):
        print('结婚')
    else:
        print('bujiehun')
    

    四.if...elif...elif 多分支

    伪代码展示
    if 条件1:
        代码1
        代码2
        代码3
    elif 条件2:
        代码1
        代码2
        代码3
    elif 条件3:
        代码1
        代码2
        代码3
    

    示例

    成绩
    score=input('你的分数:')
    score=int(score)
    
    
    if score >= 90:
        print('你牛逼')
    
    elif score >= 80:
        print('还不错')
    
    elif score >= 75:
        print('一般般')
    
    else:
        print('真差劲')
    

    五.if 嵌套

    示例

    猜年龄
    瓦文的年龄=38
    
    count = 1
    while count <= 5:
        cnl = input('输入你猜测的瓦文的年龄:').strip()
    
        if cnl.isdigit():
            cnl = int(cnl)
    
            if cnl > 瓦文的年龄:
                print('她有这么老吗?')
                print('再猜!睁大眼睛')
                print('剩余猜测次数:',count)
                count+= 1
                continue
            elif cnl < 瓦文的年龄:
                print('你是眼瞎吗?她有这么年轻???')
                print('再猜错你日子就到头了!!!')
                print('剩余猜测次数:', count)
                count+= 1
                continue
            else:
                print('有眼光啊!小伙子')
                break
        else:
            print('报警扎起来')
    
  • 相关阅读:
    非科班学习路线
    非科班秋招面试总结
    招银网络Java面经
    派分糖果
    修改CentOS7网卡名称为传统名称eth0格式
    浅谈$* 和$@的区别
    Cobbler无人值守安装
    使用kickstart + pxe 部署无人值守安装
    CentOS 7 忘记root密码解决方法
    linux系统开机流程详解
  • 原文地址:https://www.cnblogs.com/songhaixing/p/14003749.html
Copyright © 2011-2022 走看看