循环的终止语句
break    #用于完全结束一个循环,跳出循环体执行循环后面的语句
continue    #只终止本次循环,接着执行后面的循环

1. 打印0-100,截止到第6次

count = 0
while count <= 100:
print('Value: ', count)
  if count == 5:
  break
  count += 1

2. 打印0-100,截止到第6次,第6次无限循环打印

count = 0
while count <= 100:
print('Value: ', count)
if count == 5:
  continue
count += 1

3. 打印1-5,95-101

count = 0
while count <= 100:
count += 1
if count > 5 and count < 95:
  continue
print('Value: ', count)

4. 猜年龄,允许用户猜三次,中间猜对跳出循环

count = 0
age =18 while count < 3:
user_guess = int(input('Input Number: '))
if user_guess == age:
  print('Yes, you are right!')
  break
elif user_guess > age:
  print('Try smaller!')
else:
  print('Try bigger!') count += 1

5. 猜年龄,允许用户猜三次,中间猜对跳出循环,猜了三次后若没有猜对,询问是否继续,Y则继续,N则结束

count = 0
age =18 while count < 3:
user_guess = int(input('Input Number: '))
if user_guess == age:
  print('Yes, you are right!')
  break
  elif user_guess > age:
    print('Try smaller!')
  else:
    print('Try bigger!')
  count += 1 if count == 3:
  choice = input('Whether or not to continue?(Y/N): ')
  if choice == 'Y' or choice == 'y':
  count = 0
  elif choice == 'N' or choice == 'n':
  pass
  else:
  print('You must input Y/y or N/n !')
  pass

6. 帐号密码判断

ACCOUNT = 'yancy'
PASSWD = '123!' count = 0 while count < 1:
account = input('Enter your account: ')
passwd = input('Enter your password: ') if account == ACCOUNT and passwd == PASSWD:
print('Welcome to the sso system!')
break
elif account == ACCOUNT and passwd != PASSWD:
print('Your password is wrong!')
elif account != ACCOUNT:
print("Your account doesn't exist!")
count += 1 if count == 1:
while True:
choice = input('Whether not continue?(Y/N): ')
if choice == 'Y' or choice == 'y':
count = 0
break
elif choice == 'N' or choice == 'n':
print('You quit the program!')
break
else:
print("You must enter 'Y/y' or 'N/n'!")
continue

while ... else语法

当while循环正常执行完毕,中间没有被break中止,就会执行else后面的语句

count = 0
while count <= 100:
print('Value: ', count)
if count == 5:
  break #有break时,else下面的内容就不打印,没有break时,就打印。
count += 1
else:
print('loop is done.')

else语法可用于判断while程序中间是否被break中断跳出

Study 7 —— while循环中止语句的更多相关文章

  1. R语言 循环语句、分支语句和中止语句-控制流篇

    for 循环 用法 for (n in m) expr 若n在m中则运行 expr while 循环 用法 while (condition) expr 当符合condition时运行expr rep ...

  2. Python开发——【循环】语句

    while循环 while 条件: # 要执行的循环体 # 如果条件为真,那么循环体则执行 # 如果条件为假,那么循环体不执行 死循环 count = 0 while True:# 条件永远为真 pr ...

  3. JavaSE复习日记 : 循环终止语句(break/break outerFor/continue)

    最近没网,但攒了几天的博客,这次逮到机会发博客,直接三篇走起; /* * 循环终止语句: break/ break outerFor/ continue */ /* * break语句 * 1. 用于 ...

  4. JAVA_SE基础——14.循环结构语句

    建议有些基础的同学阅读,0基础可能会有些困难(最好看正文配合基础课本的例子) 所谓循环语句主要就是在满足条件的情况下反复执行某一个操作.Java提供了3种常用的循环语句,分别为for循环语句.whil ...

  5. c语言for循环等语句详解

    循环结构有: . goto语句和if语句构成循环 .while语句 .do-while语句 .for语句 goto语句 goto语句是一种无条件转移语句, 与Basic中的goto语句相似.goto语 ...

  6. shell脚本-循环选择语句

    shell脚本-循环选择语句 过程式编程语言: 顺序执行 选择执行 循环执行 注:条件中的变量,可以在执行语句中使用,不用在加上"$". if语句 根据命令的退出状态来执行命令 单 ...

  7. IT兄弟连 Java语法教程 流程控制语句 循环结构语句1

    循环语句可以在满足循环条件的情况下,反复执行某一点代码,这段被重复执行的代码被称为循环体,当反复执行这个循环体时,需要在合适的时候把循环条件该为假,从而结束循环,否则循环将一直执行下去,形成死循环.循 ...

  8. 简易计算器实现:while循环+switch语句

    个人练习: 写一个计算器,要求实现加减乘除功能,并且能循环接收新的数据,通过用户交互实现(即Scanner对象) 用到了 while循环 switch语句,实现了数据的循环输入并计算!!!!妙啊!!! ...

  9. JavaScript (If...Else和Switch和循环遍历) 语句以及常用消息框

    If...Else 语句 JavaScript中if...else语句和Java中的语法和使用方法是一样的. 只是在JavaScript中要使用小写字母.使用大写的 IF 会出错! 至于if...el ...

随机推荐

  1. Docker for windows WIN版本,主板特性问题

    WIN 10 Home版无法开启Hyper-V特性. Docker for windows有Hyper-V和VirtualBox两个版本: https://forums.docker.com/t/in ...

  2. Ehcache Monitor使用一例

    场景介绍:系统集成Shiro,使用Ehcache保存用户登录限制次数,常有用户密码被锁,影响工作效率. 在不考虑集成SSO,LDAP,也不引入身份校验,邮件,短信等解锁特性下.使用Ehcache Mo ...

  3. Appium学习笔记1_获取到APK安装包的Package以及Activity属性值

    我们设置DesiredCapabilities属性值得时候需要设置"appPackage"和"appActivity",如何获取到这两个值呢? 这两个值不是随便 ...

  4. centos7安装浏览器

    firefox(火狐) sudo yum install firefox chrome(谷歌) 添加源:sudo wget http://repo.fdzh.org/chrome/google-chr ...

  5. sleep、yield、wait、join的区别(阿里面试)

    1.  Thread.sleep(long) 和Thread.yield()都是Thread类的静态方法,在调用的时候都是Thread.sleep(long)/Thread.yield()的方式进行调 ...

  6. LoadRunner12 Java Vuser API语法举例

    // 检查点 web.reg_find("Text=\"retCode\":\"0000\"",new String[]{"FAI ...

  7. AWS、Azure和Google的云容器注册表有什么区别?

    亚马逊云计算服务(AWS).谷歌云服务和微软Azure,这三大公共云平台都提供Docker容器注册表.虽然他们的产品看起来很相似,但开发人员在做出选择之前,应该先了解价格和功能方面的差异. 公共云供应 ...

  8. BZOJ2124 等差子序列(树状数组+哈希)

    容易想到一种暴力的做法:枚举中间的位置,设该位置权值为x,如果其两边存在权值关于x对称即合法. 问题是如何快速寻找这个东西是否存在.考虑仅将该位置左边出现的权值标1.那么若在值域上若关于x对称的两权值 ...

  9. Python3网络爬虫(3):使用User Agent和代理IP隐藏身份

    Python版本: python3 IDE: pycharm2017.3.3 一.为何要设置User Agent 有一些网站不喜欢被爬虫访问,所以会检测对象,如果是爬虫程序,他就会不让你访问,通过设置 ...

  10. android studio+grade配置构建

    Android 构建系统编译应用资源和源代码,然后将它们打包成可供您测试.部署.签署和分发的 APK.android Studio 使用 Gradle 这一高级构建工具包来自动化执行和管理构建流程,同 ...