python入门基础

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

2019-01-10  本文已影响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. 
上一篇下一篇

猜你喜欢

热点阅读