循环的终止语句
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. CodeIgniter 2.x和3.x修改默认控制器问题解答

    首先明确一点,CodeIgniter框架的2.x和3.x版本中修改默认控制器是有一点区别的 但相同的操作都是修改application/config/routes.php $route['defaul ...

  2. back to top 回到顶部按钮 css+js

    效果 html <p id="back-to-top"><a href="#top"><span></span> ...

  3. Windows下面dir 总是输入成ls的一个解决方法

    转帖:http://blog.csdn.net/venusic/article/details/50543058 新建一个ls.bat文件 输入 @echo off dir 然后放到环境变量存在的一个 ...

  4. centos目录

    cd /opt cd /home/lujie cd /etc cd /usr cd /dev cd /bin cd /mnt cd /media cd /tmp

  5. 我是如何沉迷于linux系统的?

    Linux?这个对大多数人来说,是一个陌生的词.曾经的我,对这个也是一无所知的,我没有编程背景,我的专业知识是英语,而不是计算机语言. 我是如何和这个词搭上联系的呢?还是缘于一段一次奇妙的社团活动,我 ...

  6. Luogu4782 【模板】2-SAT 问题(2-SAT)

    模板.注意若x=y不一定是废话,x=0或x=0表示x必须为0.以及数组开2n. #include<iostream> #include<cstdio> #include< ...

  7. Application Server not specified

    IDEA使用tomcat启动web项目,配置页面报错Application Server not specified: 那是因为没有配置tomcat,只要配置一下就好了:

  8. bzoj1691/luogu2869 [USACO07DEC]挑剔的美食家 (STL::set)

    给牛和草都按价格排序,然后贪心地把草给牛(就是尽量给满足价格的.要求的美味度最高但不超过这个草的美味度的牛) 这个可以用一个平衡树来维护,偷懒直接用multiset了 #include<bits ...

  9. A1095. Cars on Campus

    Zhejiang University has 6 campuses and a lot of gates. From each gate we can collect the in/out time ...

  10. 牛客练习赛29 F 算式子

    https://www.nowcoder.com/acm/contest/211/F 经典题. 1.分区间 2.向下取整的值变化 & 合并相同值 #include <bits/stdc+ ...