• python流程控制


    一 简述

    """
    流程 :代码执行的过程
    控制: 对代码执行的过程进行把控
    
    三大结构:
      1.顺序结构: 默认代码从上到下一次执行
      2.分支结构: 4个分支(单项分支,双项分支,多项分支,巢状分支)
      3.循环结构: while , for

    二 分支结构

    1.1单项分支

    """
    if 条件表达式:
    code1
    code2
    ...
    如果条件表达式成立,为True,执行对应的代码块,反之,不执行;
    """

    study_bug = "小林"
    if study_bug == "小林":
        print("你的年薪最高")
        print("你的生活最好")
        print("你吃的,喝的,用的,玩的很高档")
        print("在家红旗不倒,在外彩旗飘飘")

    1.2 双项分支(二选一)

    """
    if 条件表达式:
    code1
    code2
    else:
    code3
    code4
    ...
    如果条件表达式为真,为True执行if所对应的代码块,也叫做真区间
    如果条件表达式为假,为False执行else所对应的代码块,也叫做假区间
    """

    goodman = "小明"
    if goodman == "小明":
        print("勤工俭学")
        print("必成大器")
        print("有人喜欢")
        print("贪财好色")
    else:
        print("偷鸡摸狗")
        print("奸杀掠夺")
        print("坑蒙怪骗")
        print("人面兽心")

     练习:

    """
    登录:输入账号和密码
    账号: admin 密码: 111
    输入账号密码之后,提示登录成功,反之登录失败;
    """
    input输入的所有内容,都是字符串.
    """
    res = input("先森~ 你妈贵姓:")
    print(res , type(res))
    """

    username = input("请输入您的账号:")
    password = input("请输入您的密码:")
    
    if username == "admin" and password == "111":
        print("登陆成功")
    else:
        print("登录失败")
    View Code

    1.3 多项分支

    """
    if 条件表达式1:
    code1...
    elif 条件表达式2:
    code2
    elif 条件表达式3:
    code3
    else:
    code4

    如果条件表达式1成立,执行code1代码块,反之
    如果条件表达式2成立,执行code2代码块,反之
    如果条件表达式3成立,执行code2代码块,反之
    所有条件都不满足,执行else这个分支做收尾;
    执行code4这个代码块结构

    elif 后面可以接条件,可以是0个或者多个
    else 后面不可以接条件,可以是0个或者1个
    """

    youqian = False
    youfang = False
    youche = False
    youyanzhi = True
    youtili = True
    
    if youqian == True:
        print("老娘要嫁给你1")
    elif youfang == True:
        print("老娘要嫁给你2")
    elif youche == True:
        print("老娘要嫁给你3")
    elif youyanzhi == True:
        print("老娘要嫁给你4")
    elif youtili == True:
        print("老娘要嫁给你5")
    else:
        print("你是个好人..")

    1.4 巢状分支

    """单项分支,双向分支,多项分支的互相嵌套的一种语法"""

    youqian = True
    youfang = True
    youche = False
    youyanzhi = True
    youtili = False
    
    if youqian == True:
        if youfang == True:
            if youche == True:
                if youyanzhi == True:
                    if youtili == True:
                        print("别想跑了,老娘要定了...")
                    else:
                        print("上道边,重金求子")
                else:
                    print("请你去泰国整容...")
            else:
                print("你去买了布加迪威航")
    else:
        print("你是个好人 .. ")

    练习:

    #女生找对象
    # 男生在1米~1.5米之间 小强 你在哪里?
    # 男生在1.5~1.7米之间 没有安全感~
    # 男生 1.7~ 1.8米之间 帅哥 留个电话
    # 男生 1.8~2米之间 帅哥 你建议多一个女朋友吗

    name = float(input('请输入身高'))
    if 1 <= name <= 1.5:
        print('小强在哪里')
    elif 1.5 <= naem 1.7<=:
        print('没有安全感')
    elif 1.7 < name <= 1.8:
        print('帅哥,留个电话')
    elif 1.8< name <= 2:
        print('帅哥,建议多个女朋友吗?')
    else:
        print('...')
    View Code

    1.5 循环结构

    """
    特点: 减少冗余代码,提升代码效率
    while for 两种循环

    语法:
    while 条件表达式:
    code1...

    (1) 初始化一个变量
    (2) 写上循环的条件
    (3) 写上自增自减值
    (4) 写上对应的循环逻辑
    """

    练习:

    # 打印1 ~ 100

    # 第一步
    i = 1
    # 第二步
    while i <= 100:
        # 第四步
        print(i)
        
        # 第三步
        i += 1 # i = i + 1
    
    
    解析:
    """
    首先,先初始化变量i
    然后,判断 i <= 100 条件为真, 执行循环体中的代码块
    i = 1 , print(1)
    i += 1 , i => 2
    
    回到20行,进入到循环的条件中继续判断
    i = 2 判断 i <= 100 条件为真, 执行循环体中的代码块
    i = 2 , print(2)
    i += 1 , i => 3
    
    回到20行,进入到循环的条件中继续判断
    i = 3 判断 i <= 100 条件为真, 执行循环体中的代码块
    i = 3 , print(3)
    i += 1 , i => 4
    ...
    ...
    
    当i = 101的时,判断 i <= 100 条件为假, 不执行循环体中的代码块
    到此循环结束.
    """
    View Code

    # 做1 ~ 100的累加和

    i = 1
    total = 0
    while i <= 100:
        total += i # total = total + i
        i+=1
    print(total)
    
    
    
    解析:
    """
    i = 1  i <= 100成立,执行循环中代码块
    total = total + i => total = 0 + 1
    i+=1  => i = 2
    
    回到59行,循环的判断条件中
    i = 2  i <= 100成立,执行循环中代码块
    total = total + i => total = 0 + 1 + 2
    i+=1  => i = 3
    
    回到59行,循环的判断条件中
    i = 3  i <= 100成立,执行循环中代码块
    total = total + i => total = 0 + 1 + 2 + 3
    i+=1  => i = 4
    ....
    ....
    依此类推
    i = 101  i <= 100不成立,不执行循环中代码块
    到此循环结束
    
    此时看total total = 0+1+2+3+ ..... 100 = 5050
    
    """ 
    View Code

    # 用死循环的方法完成1~100的累加和

    i = 1
    total = 0 
    sign = True
    while sign:
        total += i
        i+=1
        
        if i == 101:
            sign = False
        
    print(total)
    View Code

    # (1)打印 一行十个小星星* help(print)

    i = 1
    while i <= 10:
        # 默认打印不换行,默认补空字符串
        print("*",end="")
        i+=1
    
    # 默认打印直接换行    
    print()
    # help(print)
    View Code

    # (2)通过打印一个变量的形式,展现一行十个小星星

    i = 0
    strvar = ""
    while i<10:
        strvar += "*"
        i+=1
    print(strvar)
    print("*" * 10)
    View Code

    # (3)一行十个换色的星星 ★☆★☆★☆★☆★☆

    i = 0
    while  i < 5:
        print("★☆",end="")
        i += 1
        
    print("<========2=======>")
    i = 0
    while i < 10:
        if i % 2 == 0:
            print("",end="")
        else:
            print("",end="")
        i += 1
    print("<========3========>")
    i = 0 
    strvar = ""
    while i < 10:
        if i % 2 == 0:
            strvar += ""
        else:
            strvar += ""
        i+=1
    print(strvar)
    View Code

    # (4)用一个循环,打印十行十列小星星

    i = 0
    while i < 100:
        print("*",end="")
        if i % 10 == 9:
            print()
        i += 1
    View Code

    # (5) 一个循环实现十行十列,格列换色的小星星

    i = 0
    while i < 100:
        # 打印星星
        if i % 2 == 0:
            print("",end="")
        else:
            print("",end="")
        # 打印换行
        if i % 10 == 9:
            print()
        
        i += 1
    View Code

    # (6)一个循环实现十行十列,隔行换色的小星星

    """结论: 任意数和n进行地板除,会得到n个相同的数
        结论: 任意数和n进行地板除,会得到n个相同的数
    """
    i = 0
    while i < 100:
        # 输出星星
        if i // 10 % 2 == 0:
            print("",end="")
        else:
            print("",end="")
        
        # 打印换行
        if i % 10 == 9:
            print()
        i += 1
    View Code
  • 相关阅读:
    程序猿之歌
    How to solve the problem : &quot;You have been logged on with a temporary profile&quot;
    LeetCode Jump Game
    hdu 3496 Watch The Movie
    matlab矩阵内存预分配
    【Windows socket+IP+UDP+TCP】网络基础
    <html>
    行为类模式(十):模板方法(Template Method)
    行为类模式(九):策略(Strategy)
    行为类模式(八):状态(State)
  • 原文地址:https://www.cnblogs.com/whc6/p/14032083.html
Copyright © 2020-2023  润新知