zoukankan      html  css  js  c++  java
  • day05-Python运维开发基础(双层循环、pass/break/continue、for循环)

    # ### 双层循环练习
    
    # 十行十列小星星
    j = 0
    while j<10:
    
        # 逻辑代码写在下面
        # 打印一行十个小星星
        i = 0
        while i<10:
            print("*",end="")
            i+=1
            
        # 打印换行
        print()
    
        j+=1
    
    # 十行十列隔列换色小星星
    j = 0
    while j<10:
        # 打印星星
        i= 0
        while i<10:
            if i % 2 == 0:
                print("",end="")
            else:
                print("",end="")
            i+=1
        # 打印换行
        print()
        
        j+=1
    
    
    # 十行十列隔行换色小星星
    """j动的慢,i动的快,外面循环动一次,里面循环动10次"""
    j = 0
    while j<10:
        # 打印星星
        i= 0
        while i<10:
            if j % 2 == 0:
                print("",end="")
            else:
                print("",end="")
            i+=1
        # 打印换行
        print()
        
        j+=1
    
    """
    j = 0  j % 2 ★★★★★★★★★★
    j = 1  j % 2 ☆☆☆☆☆☆☆☆☆☆
    ...
    依次类推
    """
    
    
    # 99乘法表
    """   '%d*%d=%2d' % (值1,值2,值3) 字符串的格式化  """
    # 方向一 (正序 1~9)
    i = 1
    while i<=9:
        
        j = 1
        while j<=i:
            # print(i,j)
            # 打印对应的表达式
            print("%d*%d=%2d " % (i,j,i*j) , end="" )
            j+=1
            
        # 打印换行
        print()
        
        i+=1
    
    # 方向二 (倒序 9~1)
    print("<============>")
    i = 9
    while i>=1:
    
        j = 1
        while j<=i:
            # print(i,j)
            # 打印对应的表达式
            print("%d*%d=%2d " % (i,j,i*j) , end="" )
            j+=1
            
        # 打印换行
        print()
    
    
        i-=1
    
    print(",=============.")
    # 方向三 
    i = 1
    while i<=9:
    
        # (1)打印空格 (8 ~ 1)
        k = 9 - i
        while k>0:
            print("       ",end="")
            k-=1
        
        
        # (2)打印星星
        j = 1
        while j<=i:
            # print(i,j)
            # 打印对应的表达式
            print("%d*%d=%2d " % (i,j,i*j) , end="" )
            j+=1
            
        # (3)打印换行
        print()
        
        i+=1
        
    print("<====================>")
    # 方向四
    i = 9
    while i>=1:
    
        # (1)打印空格 (8 ~ 1)
        k = 9 - i
        while k>0:
            print("       ",end="")
            k-=1
        
        
        # (2)打印星星
        j = 1
        while j<=i:
            # print(i,j)
            # 打印对应的表达式
            print("%d*%d=%2d " % (i,j,i*j) , end="" )
            j+=1
            
        # (3)打印换行
        print()
        
        i-=1
        
        
    # 求吉利数字100 ~ 999  123 321 111 222 333 ...  666 888 567  765
    """
    765
    // 可以取到一个数高位
    %  可以取到一个数低位
    
    个位: 765 % 10  => 5
    十位: 765 // 10 % 10 => 6
    百位: 765 // 100 => 7
    """
    
    
    # 方法一:
    i = 100
    while i<=999:
        # 个位
        gewei = i % 10
        # 十位
        shiwei = i // 10 % 10
        # 百位
        baiwei = i // 100
        # print(baiwei,shiwei,gewei)
        
        # 555 666  777 
        if shiwei == gewei and  shiwei == baiwei:
            print(i)
        # 567 789
        elif shiwei == gewei - 1 and  shiwei == baiwei + 1:
            print(i)
        elif shiwei == gewei + 1 and  shiwei == baiwei - 1:
            print(i)
    
        i+=1
        
        
    # 方法二
    print("<=====================>")
    """
    strvar = "789"
    strvar[0]
    strvar[1]
    strvar[-1]
    """
    i = 100
    while i<=999:
        num = str(i)
        # 个位
        gewei = int(num[-1])
        # 十位
        shiwei = int(num[-2])
        # 百位
        baiwei = int(num[-3])
        
        # 555 666  777 
        if shiwei == gewei and  shiwei == baiwei:
            print(i)
        # 567 789
        elif shiwei == gewei - 1 and  shiwei == baiwei + 1:
            print(i)
        # 765 
        elif shiwei == gewei + 1 and  shiwei == baiwei - 1:
            print(i)
        
        
        i+=1
        
    
    # 百钱买百鸡 公鸡,母鸡,小鸡,公鸡1块钱一只,母鸡3块钱一只,小鸡是5毛钱一只,问100块钱买100只鸡,有多少种买法
    """
    穷举法:一个一个试
    a = [1,2]
    b = [3,4]
    c = [5,6]
    
    a+b+c = 10?
    1 + 3 + 5 = 9
    1 + 3 + 6 = 10 ok
    1 + 4 + 5 = 10 ok
    1 + 4 + 6 = 11
    
    2 + 3 + 5 = 10 ok
    2 + 3 + 6 = 11 
    2 + 4 + 5 = 11
    2 + 4 + 6 = 12
    
    公鸡 : x
    母鸡 : y
    小鸡 : z
    # 100只
    x+y+z = 100 
    # 100块
    x+3y+0.5z = 100
    通过and 把两个条件拼接在一起
    """
    x = 0
    count = 0
    while x<=100:
        
        y = 0
        while y<=33:    
        
            z = 0
            while z<=100:
            
                if (x+y+z == 100) and (x+3*y+0.5*z == 100):
                    count+=1
                    print(x,y,z)
                z+=1
        
            y+=1
        
        x+=1
    print(count)
    双层循环 示例代码
    # ### pass  break continue 关键字的使用
    
    # 1.pass 过 作代码的占位
    """
    if 10 % 2 == 0:
        pass
    
    while True:
        pass
    """
    # 2.break 终止当前循环 [只能是循环当中]
    # 打印1~10 遇到5就终止
    i = 1
    while i<=10:
        if i == 5:
            break
        print(i)
    
        i+=1
    
    
    i = 1
    while i<=3:
        j = 1
        while j<=3:
            if j == 2:
                break
            print(i,j)
            j+=1
        i+=1
    
    # break 必须使用在循环当中
    """    
    if 5 == 5:
        break
    """
    
    
    # 3.continue 跳过当前循环,从下一次循环继续执行
    # 1~10 除了5
    i = 1
    while i<=10:
        if i == 5:
            i+=1  # 务必自己手动加1,防止死循环
            continue
        print(i)
        i+=1
        
    # 4.打印1~100 除了含有4的数字
    """
    // 可以获取一个数的高位
    %  可以获取一个数的低位
    56 // 10 5
    56 %  10 6
    """
    
    print("<====1====>")
    # 方法一
    i = 1
    while i<=100:
        if i // 10 == 4 or i % 10 == 4:
            i+=1  # 手动加上自增加1,避免死循环
            continue
            
        print(i)
    
        i+=1
        
    print("<====2====>")
    # 方法二
    i = 1
    while i<=100:
        str_num = str(i)
        # print(str_num,type(str_num))
        if "4" in str_num:
            i+=1
            continue
        print(i)
            
        i+=1
        
    pass_break_continue 示例代码
    # ### for in 循环
    
    # 遍历 循环 迭代 把容器里的数据统统获取出来
    """
    listvar = ["a","b",1,2]
    # listvar[0]
    # listvar[1]
    # listvar[2]
    # listvar[3]
    i = 0
    while i<len(listvar):
        res = listvar[i]
        print(res)
        
        i+=1
    """
    
    # while循环在遍历数据时,存在局限性,所以for循环应用而生
    """
    # while 的局限性
    i = 0
    setvar = {"a","b","c","d"}
    while i<len(setvar):
        res = setvar[i]
        print(res)
        
        i+=1
    """
    """
    for 变量 in 可迭代性数据:
        code1
        code2...
    可迭代性数据 : (容器类型数据 , range对象 , 迭代器)
    """
    
    # 遍历字符串
    container = "有时候,打败你的仅仅是一个微笑,比如,趴在门外的班主任"
    # 遍历列表
    container = ["黄俊伟","续送课","王启军","宗永玲","刘守乱"]
    # 遍历元组
    container = ("黄俊伟","续送课","王启军","宗永玲","刘守乱")
    # 遍历集合
    container = {"黄俊伟","续送课","王启军","宗永玲","刘守乱"}
    # 遍历字典 (遍历字典时,遍历的是键)
    container = {"top":"the shy","middle":"肉鸡","bottom":"杰克love"}
    
    for i in container:
        print(i)
        
    # 变量的解包
    a,b = 1,2
    a,b = [3,4]
    a,b = "pu"
    a,b = {"a1":1,"b1":2}
    a,b,c = ["王健林","王思聪","王美丽"] 
    print(a,b,c)
        
    # 遍历等长的二级容器
    listvar = [ ["王健林","王思聪","王美丽"] , ("马云","马思聪","马文") , ("王宝强","马蓉","宋小宝")]
    
    """
    for i in listvar:
        for j in i:
            print(j)
    """
    # 优化:利用变量的解包
    for a,b,c in listvar:
        # a,b,c = ["王健林","王思聪","王美丽"]
        print(a,b,c)
    
    
    # 遍历不等长的二级容器
    listvar = [ ["王健林","王思聪","王美丽"] , ("马云","马思聪") , ("王宝强",)]
    
    for i in listvar:
        # print(i)
        for j in i:
            print(j)
        
    # range对象 : 用来遍历数据
    """
    range(start,end,step)
    start:开始值
    end  :结束值
    step :步长
    """
    # range  括号中只有一个值的情况  0~4
    for i in range(5):
        print(i)
    
    # range  括号中只有二个值的情况  1~7
    for i in range(1,8):
        print(i)
    
    # range  括号中只有三个值的情况
    # 正序
    for i in range(1,15,2):
        print(i)
    # 倒序
    for i in range(10,1,-1):
        print(i)
    
    # while写法
    i = 1
    while i<=9:
        j = 1
        while j<=i:
            print("%d*%d=%2d " % (i,j,i*j) ,end="" )
            j+=1
            
        print()
        i+=1
    # for 写法
    for i in range(1,10):
        for j in range(1,i+1):
            print("%d*%d=%2d " % (i,j,i*j) ,end="" )
        print()
    """
    while 一般用在复杂逻辑操作
    for   一般用在遍历数据操作
    但是他们之间有共同之处;
    """
    for循环 示例代码
  • 相关阅读:
    Java第十次作业-房地产;学生成绩计算
    找回感觉之模拟房地产首付计算
    软件工程第四次作业—结对项目
    【作业】需要改进的用户体验二三则
    项目互测测试方式及报告
    [ASE]项目介绍及项目跟进——TANK BATTLE·INFINITE
    Hello World!
    [ASE]sprint3 总结 & sprint4计划
    [ASE][Daily Scrum]12.15
    [ASE][Daily Scrum]12.12
  • 原文地址:https://www.cnblogs.com/reachos/p/12133454.html
Copyright © 2011-2022 走看看