美文网首页python入门基础
黑猴子的家:Python for + If 限定次数判断Pass

黑猴子的家:Python for + If 限定次数判断Pass

作者: 黑猴子的家 | 来源:发表于2019-01-10 15:38 被阅读23次

    1、打印10个数

    code

    # Author:黑猴子
    
    for i in range(10):
        print("loop",i)
    

    运行

    loop 0
    loop 1
    loop 2
    loop 3
    loop 4
    loop 5
    loop 6
    loop 7
    loop 8
    loop 9
    

    2、for循环设定步长

    code

    # Author:黑猴子
    # 设定步长为2
    for i in range(0,10,2):
        print("loop",i)
    

    运行

    loop 0
    loop 2
    loop 4
    loop 6
    loop 8
    

    3、for + If 限定次数判断Password

    code

    # Author:黑猴子
    
    age_of_heihouzi = 18
    
    for i in range(3):
        guess_age = int(input("guess age:") )
        if guess_age == age_of_heihouzi :
            print("yes, you got it. ")
            break
        elif guess_age > age_of_heihouzi:
            print("think smaller...")
        else:
            print("think bigger!")
    else:
        print("you have tried too many times..fuck off")
    

    运行

    guess age:10
    think bigger!
    guess age:19
    think smaller...
    guess age:18
    yes, you got it. 
    

    相关文章

      网友评论

        本文标题:黑猴子的家:Python for + If 限定次数判断Pass

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