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!
    

      

  • 相关阅读:
    【Android开发艺术探索】Activity的生命周期和启动模式
    【Android】基于WanAndroid开放API实现的文章阅读APP
    【Android】天气应用
    【Android】动态更新Icon
    【Android】VirtualAPK的简单使用
    Android数据存储之SD卡文件操作
    Android数据存储之SQLite数据库
    Android数据存储之共享参数SharedPreferences
    tensor维度变换
    tensor数据基操----索引与切片
  • 原文地址:https://www.cnblogs.com/googlewang/p/10665425.html
Copyright © 2011-2022 走看看