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

    一、语法

    if判断是干什么的呢?if判断其实是在模拟人做判断。就是说如果这样干什么,如果那样干什么。对于ATM系统而言,则需要判断你的账号密码的正确性。

    1.1 if

    学什么都是为了让计算机向人一样工作,我们无时无刻都在判断。路边路过一个生物,你会判断两个人是不是会表白?首先会判断这个生物是不是人类,并且这个人类是个女人,年龄大于18小于20几岁。你首先需要记录一堆数据,然后才会用你的大脑去判断。if表示if成立代码成立会干什么。

    if 条件:
    	代码1
    	代码2
    	代码3
    	...
    # 代码块(同一缩进级别的代码,例如代码1、代码2和代码3是相同缩进的代码,这三个代码组合在一起就是一个代码块,相同缩进的代码会自上而下的运行)
    
    cls = 'human'
    gender = 'female'
    age = 18
    
    if cls == 'human' and gender == 'female' and age > 16 and age < 22:
        print('开始表白')
    
    print('end...')
    
    开始表白
    end...
    

    1.2 if...else

    if 条件:
    	代码1
    	代码2
    	代码3
    	...
    else:
    	代码1
    	代码2
    	代码3
    	...
    

    if...else表示if成立代码成立会干什么,else不成立会干什么。

    cls = 'human'
    gender = 'female'
    age = 38
    
    if cls == 'human' and gender == 'female' and age > 16 and age < 22:
        print('开始表白')
    else:
        print('阿姨好')
    
    阿姨好
    

    1.3 if...elif...else

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

    if...elif...else表示if条件1成立干什么,elif条件2成立干什么,elif条件3成立干什么,elif...否则干什么。

    cls = 'human'
    gender = 'female'
    age = 28
    
    if cls == 'human' and gender == 'female' and age > 16 and age < 22:
        print('开始表白')
    elif cls == 'human' and gender == 'female' and age > 22 and age < 30:
        print('考虑下')
    else:
        print('阿姨好')
    
    考虑下
    

    二、if的嵌套

    如果我们表白的时候,表白成功的时候我们是不是会做什么,表白不成功是不是又会会做什么呢?

    # if的嵌套
    cls = 'human'
    gender = 'female'
    age = 18
    is_success = False
    
    if cls == 'human' and gender == 'female' and age > 16 and age < 22:
        print('开始表白')
        if is_success:
            print('那我们一起走吧...')
        else:
            print('我逗你玩呢')
    else:
        print('阿姨好')
    
    开始表白
    我逗你玩呢
    

    三、练习

    3.1 练习1:成绩评判

    • 如果 成绩>=90,打印"优秀"
    • 如果 成绩>=80 并且 成绩<90,打印"良好"
    • 如果 成绩>=70 并且 成绩<80,打印"普通"
    • 其他情况:打印"差"
    # 成绩评判
    score = input("your score: ")
    score = int(score)
    
    
    if score >= 90:
        print('优秀')
    # elif score >= 80 and score < 90:
    elif score >= 80:
        print('良好')
    # elif score >= 70 and score < 80:
    elif score >= 70:
        print('普通')
    else:
        print('差')
    
    your score: 80
    良好
    

    3.2 练习2:模拟登录注册

    # 模拟登录注册
    user_from_db = 'nick'
    pwd_from_db = 123
    
    user_from_inp = input('username: ')
    user_from_inp = input('password: ')
    
    if user_from_inp == user_from_db and pwd_from_inp == pwd_from_db:
        print('login successful')
    else:
        print('username or password error')
    
    username: nick
    password: 123
    username or password error
  • 相关阅读:
    SQL函数 Convert,dateadd
    WebBrowser.ExecWB的完整说明
    GetShortPathName函数
    winmm.dll包含函数
    C# 操作Excel大全
    C#操作目录和文件
    File操作
    C#中对EXCEL保存的SAVEAS方法说明
    dataTable 、dataView、Dataset 区别
    System.Windows.Forms.Application.DoEvents();
  • 原文地址:https://www.cnblogs.com/abdm-989/p/11452558.html
Copyright © 2011-2022 走看看