zoukankan      html  css  js  c++  java
  • while循环与for循环

    while循环

    while 条件 :
    代码块

    inp_age = input('>>>Please input the age :')
    inp_age = int(inp_age)
    
    if inp_age < 18:
        print("it's too small!")
    elif inp_age == 18:
        print('congratulatin!!!')
    else:
        print("it's too big!!!")
    
    >>>Please input the age :23
    it's too big!!!
    
    while True:
        inp_age = input('>>>Please input the age :')
        inp_age = int(inp_age)
        if inp_age > 18:
            print("it's too big!!!")
        elif inp_age == 18:
            print('Congratulations!!!')
            break
        else:
            print("it's too samll!!!")
    
    >>>Please input the age :16
    it's too samll!!!
    >>>Please input the age :19
    it's too big!!!
    >>>Please input the age :18
    Congratulations!!!
    
    # 三条命
    life = 0
    count = 3
    while life < count:
        inp_age = input('>>>Please input the age :')
        inp_age = int(inp_age)
        if inp_age < 18:
            print("it's too small!!!")
        elif inp_age == 18:
            print('Congratulation!!!')
            break
        else:
            print("it's too big!!!")
        life += 1
    
    >>>Please input the age :16
    it's too small!!!
    >>>Please input the age :19
    it's too big!!!
    >>>Please input the age :20
    it's too big!!!
    
    # if  , if ...else,if... elif....else ...的嵌套
    # 一次猜对得金牌,二次猜对得银牌,三次得铜牌
    
    life = 0
    count = 3
    while life < count:
        inp_age = input('>>>Please input the age :')
        inp_age = int(inp_age)
        if inp_age < 18:
            print("it's too small!")
        elif inp_age == 18:
            if life == 0:
                print('Gold medal!!!')
            elif life == 1:
                print('Silver medal!!!')
            else:
                print('Bronze medal!!!')
            break
        else:
            print("it's too big!")
        life += 1
    
    >>>Please input the age :17
    it's too small!
    >>>Please input the age :18
    Silver medal!!!
    
    # 奖品可以选择
    life = 0
    count = 3
    award_dict = {0: 'an apple', 1: 'an orange', 2: 'a banana'}
    while life < count:
        inp_age = input('>>>Please input the age :')
        inp_age = int(inp_age)
        if inp_age > 18:
            print("it's too big!")
        elif inp_age == 18:
            print(f'Congratulations!!! 
    Please choose your award:{award_dict}')
            choice = input('>>>Please input the number :')
            choice = int(choice)
            get_award = award_dict[choice]
            print(f'Your award is {get_award} ')
            break
        else:
            print("it's too small!")
        life += 1
    
    >>>Please input the age :19
    it's too big!
    >>>Please input the age :18
    Congratulations!!! 
    Please choose your award:{0: 'an apple', 1: 'an orange', 2: 'a banana'}
    >>>Please input the number :1
    Your award is an orange 
    
    # 选择多个奖品
    life = 0
    count_of_game = 3
    count_of_award = 0
    award_dict = {0: 'an apple', 1: 'an orange', 2: 'a banana'}
    while life < count_of_game:
        inp_age = input('>>>Please input the age :')
        inp_age = int(inp_age)
        if inp_age < 18:
            print("it's too small!")
        elif inp_age == 18:
            print(f'Congratulations!!! 
    Please choose your award: {award_dict}')
            while count_of_award < 3:
                choice = input('>>>Please input the number :')
                choice = int(choice)
                get_award = award_dict[choice]
                print(f'Your award is {get_award}!')
                count_of_award += 1
            break
        else:
            print("it's too big!")
        life += 1
    
    >>>Please input the age :20
    it's too big!
    >>>Please input the age :18
    Congratulations!!! 
    Please choose your award: {0: 'an apple', 1: 'an orange', 2: 'a banana'}
    >>>Please input the number :1
    Your award is an orange!
    >>>Please input the number :0
    Your award is an apple!
    >>>Please input the number :2
    Your award is a banana!
    

    while + continue(掌握)

    # 计算0+1+2....+99+100
    
    i = 0
    sum = 0
    while i < 100:
        i += 1
        sum += i
    print(sum)
    
    5050
    
    # continue 不执行下面的代码,跳出本次循环,进行下次循环。
    
    i = 0
    sum = 0
    while i < 100:
        i += 1
        if i == 20 or i == 30:
            continue
        sum += i
    print(sum)
    
    5000
    

    continue 与 break

    break直接结束while循环,如果循环100次,在50次时使用break,后面50次不在循环,结束本层循环
    continue如果循环100次,在50次时使用continue,第50次不循环,第51次继续循环,结束本次循环。

    while + else 以后尽量不要使用

    如果while循环结束,且没有被break,则执行else内代码。

    i = 0
    while i < 5:
        i += 1
        if i == 4:
            break
    else:
        print("I'm not breaking!")
    print(i)
    
    4
    
    i = 0
    while i < 5:
        i += 1
        if i == 6:
            break
    else:
        print("I'm not breaking!")
    print(i)
    
    I'm not breaking!
    5
    

    for循环

    while循环可控,但是控制不好容易死循环。

    my_info_list = ['name', 'height', 'weight', ['running', 'reading'], 'address']
    
    print(my_info_list[0])
    print(my_info_list[1])
    print(my_info_list[2])
    print(my_info_list[3])
    
    name
    height
    weight
    ['running', 'reading']
    
    my_info_list = ['name', 'height', 'weight', ['running', 'reading'], 'address']
    i = 0
    while i < len(my_info_list):
        print(my_info_list[i])
        i += 1
    
    name
    height
    weight
    ['running', 'reading']
    address
    
    my_info_list = ['name', 'height', 'weight', ['running', 'reading'], 'address']
    
    for i in my_info_list:
        print(i)
    
    name
    height
    weight
    ['running', 'reading']
    address
    
    # for + break
    my_info_list = ['name', 'height', 'weight', ['running', 'reading'], 'address']
    
    for i in my_info_list:
        if i == 'weight':
            break
        print(i)
    
    name
    height
    
    # for + continue
    my_info_list = ['name', 'height', 'weight', ['running', 'reading'], 'address']
    
    for i in my_info_list:
        if i == 'weight':
            continue
        print(i)
    
    name
    height
    ['running', 'reading']
    address
    
    # for ...else...如果for循环没有被break,则执行else代码。
    my_info_list = ['name', 'height', 'weight', ['running', 'reading'], 'address']
    
    
    for i in my_info_list:
        if i == 10:
            break
        print(i)
    else:
        print("I'm not breaking!")
    
    name
    height
    weight
    ['running', 'reading']
    address
    I'm not breaking!
    

    while 和 for 循环的异同

    相同点:都是循环
    不同点:while需要条件,for循环不需要

    for循环实现loading

    import time
    print('loading', end='')
    for i in range(6):
        print('.', end='')
        time.sleep(0.3)
    
    loading......
    

    登陆

    username = 'William'
    password = '123'
    count = 0
    choice = 0
    function_dict = {0: 'reading', 1: 'watching', 2: 'chatting', 3: 'logout'}
    while count < 3:
        inp_username = input('>>>Please input your name:')
        inp_password = input('>>>Please input your password:')
        if inp_username == username and inp_password == password:
            re_password = input('>>>Please input your password again:')
            if re_password == inp_password:
                print(
                    f'login successfully! 
    Please choose the function:{function_dict}')
                while True:
                    inp_function = int(input('>>>Please choose:'))
                    function = function_dict[inp_function]
                    if inp_function == 3:
                        print('You are logout!')
                        count = 5
                        break
                    else:
                        print(f'You are {function}')
            else:
                print('Passwords are different!')
        else:
            print('Username or password is incorrect!')
        count += 1
    
    >>>Please input your name:William
    >>>Please input your password:123
    >>>Please input your password again:123
    login successfully! 
    Please choose the function:{0: 'reading', 1: 'watching', 2: 'chatting', 3: 'logout'}
    >>>Please choose:0
    You are reading
    >>>Please choose:1
    You are watching
    >>>Please choose:1
    You are watching
    >>>Please choose:2
    You are chatting
    >>>Please choose:3
    You are logout!
    
  • 相关阅读:
    HTML常用标记(完整版)
    理论精讲-教育知识与能力7-第四章 中学生学习心理
    前端面试题总结
    for-in 和for-of循环的区别
    Nginx部署多个vue前端项目
    vue项目PC端如何适配不同分辨率屏幕
    基于Vue的项目打包为移动端app
    js中Date对象
    React Router的Route的使用
    js中数组的sort() 方法
  • 原文地址:https://www.cnblogs.com/WilliamKong94/p/10823324.html
Copyright © 2011-2022 走看看