day4 流程控制

流程控制

补充:

可以当作False来使用的:

0 None "" [] {}

 

if---
while---
for---

1.if

  • 什么是if:主要用于判断事物的对错,真假,是否可行

  • if条件:
    代码块
    gender = 'female'
    age = 25
    is_beautiful = True

    if gender =='female' and 18 < age < 23 and is_beautiful:
       print('你好')
    elif gender =='female' and 23 < age < 30 and is_beautiful:
       print('认识你很高兴')
     
    打印结果:认识你很高兴

     

  • if条件:
    代码块1
    。。。
    else:
    代码块2
    gender = 'female'
    age = 25
    is_beautiful = True

    if gender =='female' and 18 < age < 23 and is_beautiful:
       print('你好')
    else:
       print('再见')
       
    打印结果:再见
  • i f条件:
    代码块
    。。。
    elif 条件2:
    代码块2
    else:
    代码块n
    ender = 'female'
    age = 25
    is_beautiful = True

    if gender =='female' and 18 < age < 23 and is_beautiful:
       print('你好')
    elif gender == 'female' and 23 < age < 30 and is_beautiful:
       print('认识你很高兴')
    else:
       print('再见')

    模拟认证功能:
             1、接收用户的输入
             2、判断用户的输入解果
             3、返回数据

    from_db_username = 'li'
    from_db_password = '123'  # 输入字符串

    username = input('输入你的名字:')
    password = input('请输入密码:')
    if username == from_db_username and password == from_db_password:
       print('登陆成功')
    else:
       print('登陆失败')

     

  • if嵌套:
    genderender = 'female'
    age = 25
    is_beautiful = True
    is_success = True

    if gender =='female' and 18 < age < 23 and is_beautiful:
      print('你好')
      if is_success:
          print('在一起')
      else:
          print('江湖再见')
    elif gender == 'female' and 23 < age < 30 and is_beautiful:
      print('认识你很高兴')
    else:
      print('再见')

while:
        语法结构:
            while 条件:
                条件成立将要循环的代码块

 # continue:跳过本次循环,执行下一次循环  *****
        # continue下面不管有多少行代码,都不会执行
        # break:结束本层循环,单纯指代当前while  *****
        # 只能结束一层循环
        # 死循环
        count = 0
        while True:
            print(count)
            count+=1
        while+嵌套:
            from_db_password = '123'
            count = 0
            tag = True
            while tag:
                username = input("please input your username>>:")
                password = input("please input your password>>:")
                if username == from_db_username and password == from_db_password:
                    print('登录成功')
                    while tag:
                        cmd = input(">>>:")
                        if cmd == 'exit':
                            tag = ''
                        else:
                            print(f"执行{cmd}指令")
                else:
                    print("登录失败")
                    count += 1
                if count == 3:
                    print('锁定账户')
                    tag = 0
for:
    # for:给我们提供了一种不依赖于索引的取值方式
    语法结构:
        for 变量 in 容器类型:
        # 容器对象中有几个值,他就循环几次
        字典对象,直接访问无法访问值value
        for + continue
        for + break
        for + else
        # for循环正常执行结束,就会执行else对应的代码块
        # 非正常结束,例如break打断,就不会执行
    for循环的嵌套:
        for i in range(1,10):
            for j in range(1,i+1):
                print(f"{i}x{j}={i*j}",end="")
            print()
posted @ 2019-11-05 17:24  猫-园  阅读(79)  评论(0编辑  收藏  举报