zoukankan      html  css  js  c++  java
  • 第12节 while循环

    例1

    count=0
    while True:
       print("count:",count)
       count=count+1 // count+=1
    输出结果:
    count: 230033
    count: 230034
    count: 230035
    count: 230036
    count: 230037
    count: 230038
    count: 230039
    count: 230040
    

      


    例2:

    age_of_oldboy=56
    count=0
    while True:
       if count==3 :
          break
       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
    输出结果:
    guess_age:56
    yes,you got it
    

      


    例3:

    age_of_oldboy=56
    count=0
    while count<3 : //不要忘记:,在这里直接隐含着等于3的时候break,执行了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
    输出结果:
    guess_age:1
    think smaller
    guess_age:58
    think older
    guess_age:56
    yes,you got it
    

      


    例4:

    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
    else:
    print("you have tried too many times!") //如果while的条件符合 就输出while里面的,如果不符合就输出you have tried too many times!
    
    
    输出结果:
    guess_age:1
    think smaller
    guess_age:2
    think smaller
    guess_age:3
    think smaller
    you have tried too many times!
    
    输出结果:
    guess_age:1
    think smaller
    guess_age:56
    yes,you got it //没有输出you have tried too many times!
    

      

  • 相关阅读:
    Map接口框架图
    Collection接口框架图
    Java集合框架源码(四)——Vector
    Java集合框架源码(三)——arrayList
    HashSet与HashMap的区别
    Java集合框架源码(二)——hashSet
    hashMap与hashTable的区别
    HashMap与ConcurrentHashMap的区别
    asp.net 项目Net4.0 在IE10、 IE 11 下出现 “__doPostBack”未定义 的解决办法
    C# 完整List例子
  • 原文地址:https://www.cnblogs.com/googlewang/p/10665425.html
Copyright © 2011-2022 走看看