美文网首页
三 循环与判断

三 循环与判断

作者: 吃可爱长大鸭 | 来源:发表于2021-01-23 10:44 被阅读0次

    1.if判断

    # 变量
    age = 18
    # 输入密码
    ing_put = input('>>>>>....')
    # 字符转整数
    ing_put = int(ing_put)
    # if判断
    if ing_put == age:
        print('输入成功')
    else:
        print('输入失败')
    
    
    # 多变量判断
    age = 18
    name = 'gefei'
    is_start = True
    if age >= 18 and age <= 25 and name == 'gefei' and is_start:
        print('开始表白。。。。')
    else:
        print('表白失败。。。。')
    
    if判断成绩
    source = input('》》》》...')
    source = int(source)
    if source >= 80 and source <= 100:
        print('优秀')
    elif source <= 80 and source >= 60:
        print('普通')
    else:
        print('很差')
    
    # if套用
    age = 18
    name = 'gefei'
    is_start = True
    is_start1 = 123
    if age >= 18 and age <= 25 and name == 'gefei' and is_start:
        print('开始表白。。。。')
        if is_start1:
            print('一见钟情')
        else:
            print('逗你玩')
    else:
        print('表白失败。。。。')
    

    2.while 循环

    # while条件循环
    name_stg = 'mzk'
    passwd_stg = '000'
    tag = True
    while tag:
        name_st = input('请输入你的账户名>>>')
        passwd_st = input('请输入你的你密码>>>')
        if name_st == name_stg and passwd_st == passwd_stg:
            print('输入正确。。。')
            tag = False
        else:
            print('输入不正确')
    
    
    # while+break代表结束本层循环
    name_stg = 'mzk'
    passwd_stg = '000'
    while True:
        name_st = input('请输入你的账户名>>>')
        passwd_st = input('请输入你的你密码>>>')
        if name_st == name_stg and passwd_st == passwd_stg:
            print('输入正确。。。')
            break
        else:
            print('输入不正确')
    

    3.for 循环

    #while循环 VS for循环
    #1.
    #while循环:称之为条件循环,循环的次数取决于条件何时为False
    #for循环:称之为...循环,循环的次数取决于数据的包含的元素的个数
    
    #2.
    #for循环专门用来取值,在循环取值方面比while循环要强大,以后但凡
    #遇到循环取值的场景,就应该用for循环
    
    #  0   1    2
    l=['a','b','c']
    for i in range(3):
        print(i,l[i])
    
    #for+break
    names=['egon','kevin','alex','hulaoshi']
    for name in names:
        if name == 'alex':break
        print(name)
    
    #for+continue
    names=['egon','kevin','alex','hulaoshi']
    for name in names:
        if name == 'alex':continue
        print(name)
    
    #for+else
    names=['egon','kevin','alex','hulaoshi']
    for name in names:
        # if name == 'alex':break
        print(name)
    else:
        print('=====>')
    
    for循环嵌套
    for i in range(3): #i=2
        for j in range(2): #j=1
            print(i,j) #2,1
    

    相关文章

      网友评论

          本文标题:三 循环与判断

          本文链接:https://www.haomeiwen.com/subject/jjjdzktx.html