• python之流程控制


    一、流程控制之if...else

    ​ 既然我们编程的目的是为了控制计算机能够像人脑一样工作,那么人脑能做什么,就需要程序中有相应的机制去模拟。人脑无非是数学运算和逻辑运算,对于数学运算在上一节我们已经说过了。对于逻辑运算,即人根据外部条件的变化而做出不同的反映,比如:

    1 如果:女人的年龄>30岁,那么:叫阿姨

    age_of_girl=31
    if age_of_girl > 30:
        print('阿姨好')
    

    2 如果:女人的年龄>30岁,那么:叫阿姨,否则:叫小姐

    age_of_girl=18
    if age_of_girl > 30:
        print('阿姨好')
    else:
        print('小姐好')
    

    3 如果:女人的年龄>=18并且<22岁并且身高>170并且体重<100并且是漂亮的,那么:表白,否则:叫阿姨

    age_of_girl=18
    height=171
    weight=99
    is_pretty=True
    if age_of_girl >= 18 and age_of_girl < 22 and height > 170 and weight < 100 and is_pretty == True:
        print('表白...')else:
        print('阿姨好')
    
    #在表白的基础上继续:
    #如果表白成功,那么:在一起
    #否则:打印。。。
    
    age_of_girl=18
    height=171
    weight=99
    is_pretty=True
    
    success=False
    
    if age_of_girl >= 18 and age_of_girl < 22 and height > 170 and weight < 100 and is_pretty == True:
        if success:
            print('表白成功,在一起')
        else:
            print('什么爱情不爱情的,爱nmlgb的爱情,爱nmlg啊...')
    else:
        print('阿姨好')
    

    4 如果:成绩>=90,那么:优秀

    如果成绩>=80且<90,那么:良好

    如果成绩>=70且<80,那么:普通

    其他情况:很差

    score=input('>>: ')
    score=int(score)
    
    if score >= 90:
        print('优秀')
    elif score >= 80:
        print('良好')
    elif score >= 70:
        print('普通')
    else:
        print('很差')
    
    if 条件1:
    
        缩进的代码块
    
      elif 条件2:
    
        缩进的代码块
    
      elif 条件3:
    
        缩进的代码块
    
      ......
    
      else:  
    
        缩进的代码块
    
    用户登录验证练习
    name=input('请输入用户名字:')
    password=input('请输入密码:')
    
    if name == 'egon' and password == '123':
        print('egon login success')
    else:
        print('用户名或密码错误')
    
    #根据用户输入内容打印其权限
    
    '''
    egon --> 超级管理员
    tom  --> 普通管理员
    jack,rain --> 业务主管
    其他 --> 普通用户
    '''
    name=input('请输入用户名字:')
    
    if name == 'egon':
        print('超级管理员')
    elif name == 'tom':
        print('普通管理员')
    elif name == 'jack' or name == 'rain':
        print('业务主管')
    else:
        print('普通用户')
    
    # 如果:今天是Monday,那么:上班
    # 如果:今天是Tuesday,那么:上班
    # 如果:今天是Wednesday,那么:上班
    # 如果:今天是Thursday,那么:上班
    # 如果:今天是Friday,那么:上班
    # 如果:今天是Saturday,那么:出去浪
    # 如果:今天是Sunday,那么:出去浪
    
    
    #方式一:
    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':
        print('出去浪')
    elif today == 'Sunday':
        print('出去浪')
    else:
        print('''必须输入其中一种:
        Monday
        Tuesday
        Wednesday
        Thursday
        Friday
        Saturday
        Sunday
        ''')
    

    二、流程控制之while循环

    while 条件:    
        # 循环体
     
        # 如果条件为真,那么循环体则执行,执行完毕后再次循环,重新判断条件。。。
        # 如果条件为假,那么循环体不执行,循环终止
    
    #打印0-10
    count=0
    while count <= 10:
        print('loop',count)
        count+=1
    
    #打印0-10之间的偶数
    count=0
    while count <= 10:
        if count%2 == 0:
            print('loop',count)
        count+=1
    
    #打印0-10之间的奇数
    count=0
    while count <= 10:
        if count%2 == 1:
            print('loop',count)
        count+=1
    

    死循环

    import time
    num=0
    while True:
        print('count',num)
        time.sleep(1)
        num+=1 
    

    循环嵌套与tag

    tag=True 
    
      while tag:
    
        ......
    
        while tag:
    
          ........
    
          while tag:
    
            tag=False
    

    break与continue

    #break用于退出本层循环
    while True:
        print "123"
        break
        print "456"
    
    #continue用于退出本次循环,继续下一次循环
    while True:
        print "123"
        continue
        print "456"
    
    #与其它语言else 一般只与if 搭配不同,在Python 中还有个while ...else 语句,while 后面的else 作用是指,当while 循环正常执行完,中间没有被break 中止的话,就会执行else后面的语句
    count = 0
    while count <= 5 :
        count += 1
        print("Loop",count)
    
    else:
        print("循环正常执行完啦")
    print("-----out of while loop ------")
    输出
    Loop 1
    Loop 2
    Loop 3
    Loop 4
    Loop 5
    Loop 6
    循环正常执行完啦
    -----out of while loop ------
    
    #如果执行过程中被break啦,就不会执行else的语句啦
    count = 0
    while count <= 5 :
        count += 1
        if count == 3:break
        print("Loop",count)
    
    else:
        print("循环正常执行完啦")
    print("-----out of while loop ------")
    输出
    
    Loop 1
    Loop 2
    -----out of while loop ------
    

    三、程控制之for循环

    1 迭代式循环:for,语法如下

      for i in range(10):

        缩进的代码块

    2 break与continue(同上)

    3 循环嵌套

  • 相关阅读:
    win10 激活
    window10 将程序的快捷方式加到右键"发送到"
    UML 类图基本了解
    php move_uploaded_file 上传的文件移动带中文文件名的的问题
    php 的 PHPExcel1.8.0 使用
    msyql 数据类型存储大小及数据范围
    MySQL数据库设计规范
    测试工具
    show tables 或者 show databases 结果太多如何再筛选
    ubuntu 16.04 周期定时任务 crontab 的 使用
  • 原文地址:https://www.cnblogs.com/fuwei8086/p/10574792.html
Copyright © 2020-2023  润新知