第13节 FOR循环

for i in range(10): //循环10次,每次加1 赋值给i
    print("loop",i)


输出结果
loop 0
loop 1
loop 2
loop 3
loop 4
loop 5
loop 6
loop 7
loop 8
loop 9

 

  

只打印偶数

for i in range(0,10,2): //循环10次,每次加1 赋值给i
    print("loop",i)


输出结果
loop 0

loop 2

loop 4

loop 6

loop 8

  


隔2个打印一个

for i in range(0,10,3): //循环10次,每次加1 赋值给i
    print("loop",i)


输出结果
loop 0

loop 3


loop 6

loop 9

  

例1:

age_of_oldboy=56

for i in range(3):
   guess_age=int(input("guess_age:"))
   if age_of_oldboy==guess_age:
      print("yes,you got it")
      break
   elif age_of_oldboy>guess_age:
       print("think smaller")
   else:
       print("think older")
else:
print("you have tried too many times!")

  




例2:

age_of_oldboy=56
count=0
while count<3 :
    guess_age=int(input("guess_age:"))
    if age_of_oldboy==guess_age:
       print("yes,you got it")
       break
    elif age_of_oldboy>guess_age:
       print("think smaller")
    else:
       print("think older")
    count+=1
  if count==3: #进行判断
    continue_confirm=input("do you want to try again?")
    if continue_confirm!="n": #等于n的时候退出,不等于n
       count=0 #进行重置

  

posted on 2019-04-07 14:59  googleWang  阅读(154)  评论(0编辑  收藏  举报

导航