• 流程控制(if、while、for)



    一、if判断

    python中使用缩进来区分代码块

       

          语法:
    1、if 条件:
    子代码块

    2、if 条件:
    条件成立执行代子码块
    子代码块

    else:
    条件不成立执行代子码块
    子代码块

    练习:
    sex = "female"
    age = 18
    is_beatuiful = True
    is_successful = True
    height = 1.70

    if sex == "female" and age >16 and age <24 and is_beatuiful and height > 1.60 and height <1.8:
        print("开始表白")
            if is_successful:
                print("在一起。。。")
            else:
                print("什么狗屁爱情")
    else:
        print("独自深夜买醉")



    3、套嵌
    if 条件:
        if 条件:
        else:
    else:

    4、if 条件一:
         elif:条件二:
         else:

    练习:
    学生输入成绩,对学生成绩进行评价 [90-100] 为优秀,[80,90)为良好,
    [70,80)为普通,低于七十分为很差。


    grades = int(input("请输入你的成绩>>>"))
    if grades >= 90:
        print('优秀')
    elif grades >= 80:
        print('良好')
    elif grades >= 70:
        print('普通')
    else:
        print('很差')


    二、while循环

    千万不要在while循环中写带有运算类的代码:


    示例:
    while True:
        name = input("pleasse input your name: ")
        pwd = input("pleasse input your pwd")


        if name == "song" and pwd == "123"
            print("log in success")
        else:
            print("Username or password is inconrrect")


    结束方式1:条件改为False

    在条件改为False是不会立即结束掉循环,而是要等到下一次循环判断条件时才会生效

    示例:
    tag=True
    while tag:
        name=input("please input your name: ")
        pwd=input("please input your pwd: ")

        if name == "song" and pwd == "123"
            input("login successful")
            tag=False
        else:
            print("username or password errror")

    print("11111111")


    结束方式2:while+break

    break一定要放在循环体内,一旦循环体执行到break就会立即结束本层循环

    示例:
    while True:
        name = input("please input your name: ")
        pwd = input("please input youer pwd: ")
        if name == "song" and pwd == "123":
            print("login successful")
            break
        else:
            print("username or passwoed error")

    print("1111111")


    结束方式3:while+continue:结束本次循环,进入下一次循环

    示例1:
    count = 1
    while count < 6
        if count == 4
            count += 1
            continue

        print("count")
        count += 1


    示例2:
    while True:
        name = input("please input your name: ")
        pwd = input("please input your name")
       

        if name == "song" and pwd == "123"
            print("login successful")
            break
        else:
            print("Username or password error")
            continue      此处加continue无用


    3、for循环

    for循环能做的事while循环都能做,for循环的强大之处在于循环取值。

    l=["a","b","c","d","e",]

    用while取值步骤:
    i=0
    while i < len(i):
        print(l[i])
        i +=1
    而for的取值步骤:
    for x in l:
        print(x)


    for+range

    range的用法:
    range(1,5)=range(1,5,1)=range[1,2,3,4]

    for i in range(1,5):
        print(i)

    会得出结果:1 2 3 4

  • 相关阅读:
    linux可执行文件添加到PATH环境变量的方法
    PHPExcel所遇到问题的知识点总结
    如何查看已经安装的nginx、apache、mysql和php的编译参数
    oracle 创建用户及表空间命令
    datetimepicker 设置日期格式、初始化
    Linux 修改系统时间(自动同步)
    Nginx 负载均衡配置
    CenterOS7 安装 Nginx【转】
    java https post请求并忽略证书,参数放在body中
    将.cer证书导入java密钥库?
  • 原文地址:https://www.cnblogs.com/wangyisen/p/10575299.html
Copyright © 2020-2023  润新知