zoukankan      html  css  js  c++  java
  • 双循环案例和pass、break、continue、for循环、range函数

    1、双循环案例练习

    1.1 十行十列小星星

    while j < 10:
        i = 0
        while i < 10:
            print("*",end ="")
            i += 1
        print()
        j += 1
    # **********
    # **********
    # **********
    # **********
    # **********
    # **********
    # **********
    # **********
    # **********
    # **********

    1.2 十行十列隔列换色小星星

    j = 0
    while j < 10:
        i = 0
        while i <10:
            if i %2 ==0:
                print("",end="")
            else:
                print("",end="")
            i += 1
        print()
        j += 1
    # ☆★☆★☆★☆★☆★
    # ☆★☆★☆★☆★☆★
    # ☆★☆★☆★☆★☆★
    # ☆★☆★☆★☆★☆★
    # ☆★☆★☆★☆★☆★
    # ☆★☆★☆★☆★☆★
    # ☆★☆★☆★☆★☆★
    # ☆★☆★☆★☆★☆★
    # ☆★☆★☆★☆★☆★
    # ☆★☆★☆★☆★☆★

    1.3 十行十列隔行换色小星星

    j = 0
    while j < 10:
        i = 0
        while i < 10:
            if j % 2 == 0:
                print("",end = "")
            else:
                print("",end = "")
            i += 1
        print()
        j += 1

    1.4 99乘法表

    1.4.1 

    i = 1
    while i <=9:
        j = 1
        while j <= i:
            print("%d*%d=%2d "%(i,j,i*j),end = "")
            j += 1
        print()
        i += 1
    # 1*1= 1 
    # 2*1= 2 2*2= 4
    # 3*1= 3 3*2= 6 3*3= 9
    # 4*1= 4 4*2= 8 4*3=12 4*4=16
    # 5*1= 5 5*2=10 5*3=15 5*4=20 5*5=25
    # 6*1= 6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
    # 7*1= 7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
    # 8*1= 8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
    # 9*1= 9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81

    1.4.2

    i = 9
    while i >= 1:
        j = 1
        while j <= i:
            print("%d*%d=%-2d "%(i,j,i*j),end = "")
            j += 1
        print()
        i -= 1
    # 9*1=9  9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 
    # 8*1=8  8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 
    # 7*1=7  7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 
    # 6*1=6  6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 
    # 5*1=5  5*2=10 5*3=15 5*4=20 5*5=25 
    # 4*1=4  4*2=8  4*3=12 4*4=16 
    # 3*1=3  3*2=6  3*3=9  
    # 2*1=2  2*2=4  
    # 1*1=1

    1.4.3

    i = 1
    while i <= 9:
        k = 9-i
        while k > 0:
            print("       ",end="")
            k -=1
        j = 1
        while j <= i:
            print("%s*%s=%2d "%(i,j,i*j),end="")
            j += 1
        print()
        i += 1
    #                                                         1*1= 1 
    #                                                  2*1= 2 2*2= 4 
    #                                           3*1= 3 3*2= 6 3*3= 9 
    #                                    4*1= 4 4*2= 8 4*3=12 4*4=16 
    #                             5*1= 5 5*2=10 5*3=15 5*4=20 5*5=25 
    #                      6*1= 6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 
    #               7*1= 7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 
    #        8*1= 8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 
    # 9*1= 9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 

    1.4.4

    i = 9
    while i> 0:
        k = 9 - i
        while k > 0:
            print("       ",end="")
            k -= 1
        j = 1
        while j <= i:
            print("%s*%s=%2d "%(i,j,i*j),end="")
            j += 1
        print()
        i -= 1
    # 9*1= 9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 
    #        8*1= 8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 
    #               7*1= 7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 
    #                      6*1= 6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 
    #                             5*1= 5 5*2=10 5*3=15 5*4=20 5*5=25 
    #                                    4*1= 4 4*2= 8 4*3=12 4*4=16 
    #                                           3*1= 3 3*2= 6 3*3= 9 
    #                                                  2*1= 2 2*2= 4 
    #                                                         1*1= 1

    1.5 在100~999中求吉利数字

    方法一:

    i = 100
    while i <= 999:
        gewei = i % 10
        shiwei = i // 10 % 10
        baiwei = i // 100
        if gewei == shiwei and shiwei == baiwei:
            print(i)
        if gewei == shiwei - 1 and shiwei == baiwei -1:
            print(i)
        if gewei == shiwei + 1 and shiwei == baiwei + 1:
            print(i)
        i += 1

    方式二:字符串方式

    i = 100
    while i <= 999:
        var = str(i)
        a = int(var[0])
        b = int(var[1])
        c = int(var[-1])
        if a == b and b == c:
            print(int(var))
        if a == b +1 and b == c +1:
            print(int(var))
        if a == b-1 and b == c-1:
            print(int(var))
        i += 1

    1.6 公鸡1块钱1只,母鸡3块钱一只,小鸡5毛钱一只

    问: 用100块钱买100只鸡,有多少种买法?

    x = 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:
                    print(x,y,z)
                z += 1
            y+= 1
        x +=1

     2、pass、break、continue

    2.1 pass:表示占位,无代码块可写时,表示过

    if 5 == 5:
    pass

    2.2 break,表示终止本层循环

    if 5 == 5:
        pass
    i = 0
    while i < 10:
        print(i)
        if i == 5:
            break
        i += 1  #单层循环
    
    i =0
    while i < 8:
        print(i)
        j = 0
        while j < 5:
            print(j)
            if j == 2:
                break
            j += 1
        i += 1  # 仅终止掉本层循环

    2.3 continue 表示终止本次循环,进入下一次循环

    i = 0
    while i < 10:
        if i == 8:
            i += 1  # 手动加1.防止进入死循环
            continue
        print("我是" + str(i))
        i += 1
    
    # 打印 1 ~ 100 所有不含有4的数字
    # 方式一:
    i = 0
    while i <= 100:
        if i % 10 == 4 or i // 10 == 4 or i == 4:
            i += 1
            continue
        print(i)
        i += 1
    # 方式二:
    i = 0
    while i <= 100:
        if "4" in str(i):
            i += 1
            continue
        print(i)
        i += 1

    3、for循环:可用于遍历容器内的元素(可迭代的对象包括 容器类型的数据、range函数、迭代器)

    str1 = "我是一个字符串"
    for i in str1:
        print(i,end="")
    print(type(i))
    # 我是一个字符串<class 'str'>
    
    list1 = ["我是","列表"]
    for i1 in list1:
        print(i1,type(i1))
    # 我是 <class 'str'>
    # 列表 <class 'str'>
    
    tuple1 = ("我是","元祖")
    for i2 in tuple1:
        print(i2)
    
    set1 = {"我是","集合"}
    for i3 in set1:
        print(i3) # 注意集合的无序性
    
    dict1 = {"我是":"字典"}
    for i4 in dict1:
        print(i4) # 我是 仅打印字典的键

    4、range函数:

    # range函数
    # range(start,end,step) 顾头不顾尾
    for i in range(1,10,2):
        print(i)  # 1 3 5 7 9
    
    for i in range(1,10):
        for j in range(1,i+1):
            print("%d*%d=%2d "%(i,j,i*j),end="")
            j += 1
        print()
        i += 1
  • 相关阅读:
    腾讯安全上海游戏部门笔试题
    2017
    2016
    2015
    2014
    2013
    2012
    2011
    2010
    2009
  • 原文地址:https://www.cnblogs.com/fdsimin/p/12836805.html
Copyright © 2011-2022 走看看