• if、while、for循环


    一.if判断

    1.if...else

    cls = 'human'
    gender = 'female'
    age = 38
    
    if cls == 'human' and gender == 'female' and age >16 and age < 22:
        print('表白开始')
    else:
        print('阿姨好')
    
    阿姨好
    

    2.if...elif...else

    cls = 'human'
    gender = 'female'
    age = 28
    
    if cls == 'human' and gender == 'female' and age > 16 and age < 25:
        print('开始表白')
    elif cls == 'human' and gender == 'female' and age > 25 and age < 30:
        print('考虑下')
    else:
        print('阿姨好')
    
    考虑下
    

    3.if嵌套

    cls = 'human'
    gender = 'female'
    age = 18
    is_success = False
    
    if cls == 'human' and gender == 'female' and age > 16 and age < 22:
        print('开始表白')
        if is_success:
            print('那我们一起走吧...')
        else:
            print('我逗你玩呢')
    else:
        print('阿姨好')
    
    开始表白
    我逗你玩呢
    

    练习

    engo_age=73
    age=int(input('请猜年龄:'))
    if age==engo_age:
        print('猜对了')
    elif age>engo_age:
        print('猜大了')
    elif age<engo_age:
        print("33[1;31;43m猜小了33[0m")
    
    请猜年龄:4
    猜小了
    
    engo_age=73
    age=int(input('请输入年龄:'))
    if age!=engo_age:
        if age>engo_age:
            print('猜大了')
        else:
            print('猜小了')
    else:
        print('猜对了')
    
    请输入年龄:511
    猜大了
    

    二.while循环

    1.while+break

    while True:
        engo_age=73
        age=int(input('请输入年龄:'))
        if age!=engo_age:
            if age>engo_age:
                print('猜大了')
            else:
                print('猜小了')
        else:
            print('猜对了')
            break
    
    请输入年龄:45
    猜小了
    请输入年龄:73
    猜对了
    

    break的意思是终止当前循环,执行其他代码

    2.while+continue

    n = 1
    while n < 10:
        if n == 8:
            n += 1   # 如果注释这一行,则会进入死循环
            continue
        print(n)
        n += 1
    
    1
    2
    3
    4
    5
    6
    7
    9
    

    continue的意思是终止本次循环,直接进入下一次循环

    3.while循环的嵌套

    while True:
        user_db = 'xiaoming'
        pwd_db = '123'
    
        inp_user = input('username:')
        inp_pwd = input('password:')
    
        if inp_user == user_db and pwd_db == inp_pwd:
            print('login successful')
            while True:
                cmd = input('请输入你需要的命令:')
                if cmd == 'q':
                    break
                print(f'{cmd}功能执行')
            break
        else:
            print('username or password error')
    print('退出了while循环')
    
    username:xiaoming
    password:123
    login successful
    请输入你需要的命令:q
    退出了while循环
    

    三.for循环

    1.for+break

    name_list = ['xiaoming','mark','tony','nini']
    for name in name_list:
        if name == 'tony':
            break
        print(name)
    
    xiaoming
    mark
    

    2.for+continue

    name_list = ['xiaoming','mark','tony','nini']
    for name in name_list:
        if name == 'mark':
            continue
        print(name)
    
    xiaoming
    tony
    nini
    

    3.for循环嵌套

    for i in range(3):
        print(f'---:{i}')
        for j in range(2):
            print(f'***:{j}')
    
    ---:0
    ***:0
    ***:1
    ---:1
    ***:0
    ***:1
    ---:2
    ***:0
    ***:1
    

    练习1:实现loading加载效果

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

    练习2:不改变列表地址,把列表内容颠倒

    s=['h','e','l','l','o']
    for i in range(len(s)//2):
        s[i],s[len(s)-i-1]=s[len(s)-i-1],s[i]
    print(s)
    
    ['o', 'l', 'l', 'e', 'h']
    
  • 相关阅读:
    nacos 配置优先级
    spring cloud 依赖查询
    树-数据结构
    CI/CD + docker 综合实战
    CICD:CentOS 下 Jenkins 安装
    生产环境 OOM 与 GC 问题的处理思路
    如何优雅的进行接口管理
    合并多个Execl 电子表格 java poi
    深入了解数据导入的一些解决方案
    浅谈导出Execl的报表数据解决方案
  • 原文地址:https://www.cnblogs.com/gaohuayan/p/10907911.html
Copyright © 2020-2023  润新知