• python之浅谈循环


    流程控制之while循环

    循环:当你需要重复一件事,就叫循环

    while循环允许以布尔条件的重复测试为基础的一般重复

    语法

    while True:
        print('hello world')
    '''
    while(当) <条件>:
        <需要进行重复的代码块>  # 当条件成立时会进行运行,结束完代码块后会再一次判断条件,成立再运行,运行完再判断条件,%
    '''
    

    while+break

    while True:
        print('hello world')
        break
    '''
    while(当) <条件>:
        <需要进行重复的代码块>  # 当条件成立时会进行运行,结束完代码块后会再一次判断条件,成立再运行,运行完再判断条件,%break  # 遇到break后终止while循环
    '''    
    

    hello world

    while+continue

    count=0
    while count<10:
       count += 1
       if count==6:
            continue        #跳出本次循环
       print(count,end=' ') #end=''为不换行
    
            
    

    1 2 3 4 5 7 8 9 10

    while+else

    count=0
    while count<10:
        print(count)
        count+=1
    else:
        print('结束循环')
    

    0 1 2 3 4 5 6 7 8 9 结束循环

    while循环的嵌套

    age=25
    count=0
    while count<3:        #外层循环,控制是否还想继续
        while count< 3:   #内层循环,控制游戏次数
            guess = input("猜测:")
            if int(guess) > age:
                print("猜大了")
            elif int(guess) < age:
                print("猜小了")
            else:
                print("33[41m恭喜你猜对了33[0m".center(30))     
                count=9  #控制跳出程序
                break                      
            count += 1
        if  count==3:  #判断是否需要执行下面的代码
            rode = input('还想继续吗')
            if rode=='y' or rode=='Y':
              count=0
              print('请继续:')
            elif rode=='n' or rode=='N':
              print('拜拜')
              break
    

    流程控制之for循环

    for循环对定义序列(如字符串中的字符、列表中的元素或一定范围内的数字)的值提供了适当的迭代

    语法

     for i in range(11):
         print(i,end=' ')
    '''
    for  <变量>   in   <循环序列>:
            【循环体】
    ''' 
    
    

    0 1 2 3 4 5 6 7 8 9 10

    for+break

    for i in range(0,12):
        print(i,end=' ')
        if i==10:
            print('bai')
            break
    #碰到break后终止for循环
    

    0 1 2 3 4 5 6 7 8 9 10 bai

    for+continue

    for i in range(0,14):
        if i==12:
            continue
        print(i, end=' ')
         #碰到continue则终止本次循环
         
    

    0 1 2 3 4 5 6 7 8 9 10 11 13

    for+else

    for i in range(0,16,4):
        print(i,end=' ')
    else:
        print('结束循环')
    
    

    0 4 8 12 结束循环

    for循环的嵌套

    for i in range(1, 10):  #循环列
        for j in range(1, i+1):   #循环行
            print(f'{j}*{i}={i*j}	', end='')
        print()
    
    '''
    1*1=1	
    1*2=2	2*2=4	
    1*3=3	2*3=6	3*3=9	
    1*4=4	2*4=8	3*4=12	4*4=16	
    1*5=5	2*5=10	3*5=15	4*5=20	5*5=25	
    1*6=6	2*6=12	3*6=18	4*6=24	5*6=30	6*6=36	
    1*7=7	2*7=14	3*7=21	4*7=28	5*7=35	6*7=42	7*7=49	
    1*8=8	2*8=16	3*8=24	4*8=32	5*8=40	6*8=48	7*8=56	8*8=64	
    1*9=9	2*9=18	3*9=27	4*9=36	5*9=45	6*9=54	7*9=63	8*9=72	9*9=81	
    '''
    

    while循环与for循环的区别

    while:

    ​ 1.会进入死循环(不可控)

    ​ 2.世间万物都可以作为循环的对象

    for:

    ​ 1.不会进入死循环(可控)

    ​ 2.只对容器类数据类型+字符串循环

  • 相关阅读:
    java面向对象第三章
    java基础(9)
    java基础(8)
    java基础(7)
    java基础(6)
    Java(20)file i/o
    Java(19)JDBC
    Java(18) 集合框架
    Java(17)异常
    Java(16)接口
  • 原文地址:https://www.cnblogs.com/MrYang161/p/11284635.html
Copyright © 2020-2023  润新知