Study 7 —— while循环中止语句
循环的终止语句
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循环中止语句的更多相关文章
- R语言 循环语句、分支语句和中止语句-控制流篇
for 循环 用法 for (n in m) expr 若n在m中则运行 expr while 循环 用法 while (condition) expr 当符合condition时运行expr rep ...
- Python开发——【循环】语句
while循环 while 条件: # 要执行的循环体 # 如果条件为真,那么循环体则执行 # 如果条件为假,那么循环体不执行 死循环 count = 0 while True:# 条件永远为真 pr ...
- JavaSE复习日记 : 循环终止语句(break/break outerFor/continue)
最近没网,但攒了几天的博客,这次逮到机会发博客,直接三篇走起; /* * 循环终止语句: break/ break outerFor/ continue */ /* * break语句 * 1. 用于 ...
- JAVA_SE基础——14.循环结构语句
建议有些基础的同学阅读,0基础可能会有些困难(最好看正文配合基础课本的例子) 所谓循环语句主要就是在满足条件的情况下反复执行某一个操作.Java提供了3种常用的循环语句,分别为for循环语句.whil ...
- c语言for循环等语句详解
循环结构有: . goto语句和if语句构成循环 .while语句 .do-while语句 .for语句 goto语句 goto语句是一种无条件转移语句, 与Basic中的goto语句相似.goto语 ...
- shell脚本-循环选择语句
shell脚本-循环选择语句 过程式编程语言: 顺序执行 选择执行 循环执行 注:条件中的变量,可以在执行语句中使用,不用在加上"$". if语句 根据命令的退出状态来执行命令 单 ...
- IT兄弟连 Java语法教程 流程控制语句 循环结构语句1
循环语句可以在满足循环条件的情况下,反复执行某一点代码,这段被重复执行的代码被称为循环体,当反复执行这个循环体时,需要在合适的时候把循环条件该为假,从而结束循环,否则循环将一直执行下去,形成死循环.循 ...
- 简易计算器实现:while循环+switch语句
个人练习: 写一个计算器,要求实现加减乘除功能,并且能循环接收新的数据,通过用户交互实现(即Scanner对象) 用到了 while循环 switch语句,实现了数据的循环输入并计算!!!!妙啊!!! ...
- JavaScript (If...Else和Switch和循环遍历) 语句以及常用消息框
If...Else 语句 JavaScript中if...else语句和Java中的语法和使用方法是一样的. 只是在JavaScript中要使用小写字母.使用大写的 IF 会出错! 至于if...el ...
随机推荐
- Linux 重启网络提示找不到eth0(no device found for “System eth0”)
一.背景 使用VMWare创建了一个虚拟机(VM1),然后通过拷贝的方式创建了另一台虚拟机(VM2).在第二台虚拟机上设置网卡为固定IP,使用service network restart重启网络的时 ...
- Angular service定义服务
<!DOCTYPE html><html ng-app="myApp"><head lang="en"> <meta ...
- width() 、 height() 方法;innerWidth() 、innerHeight() 方法;outerWidth() 、 outerHeight() 方法的区别
1.width() . height() 方法 设置或返回元素的宽度.高度(不包括内边距.边框或外边距): 2.innerWidth() .innerHeight() 方法 返回元素的宽度.高度(包括 ...
- Bootstrap排版——HTML元素的样式重定义
前面的话 Bootstrap对默认的HTML元素进行了CSS样式定义,使得各种基本结构套用出来的HTML页面更加美观.本文将详细介绍Bootstrap中排版相关的内容 标题 [h] HTML 中的所有 ...
- Qt ------ 覆盖eventFilter(),捕获组件事件,事件处理
在Qt中,当一个事件发生时(例如鼠标点击或某个键盘上的按键按下),其传递顺序如图所示.从这个图可以看出,事件过滤器首先获得事件,其次才是部件的 event 函数,最后是部件的事件处理函数 事件过滤器由 ...
- php-编译模块2
PHP扩展-扩展的生成和编译 首先说明一下,PHP扩展有两种编译方式:方式一:在编译PHP时直接将扩展编译进去方式二:扩展被编译成.so文件,在php.ini里配置加载路径: 以下开始说明创建PHP扩 ...
- 关于mysql性能压测之tpcc
软件下载: wget http://imysql.com/wp-content/uploads/2014/09/tpcc-mysql-src.tgz安装依赖:yum install -y mysql- ...
- POJ 1661 (Help Jimmy )
Help Jimmy Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13669 Accepted: 4541 Descr ...
- codeforces 777C
C.Alyona and Spreadsheet During the lesson small girl Alyona works with one famous spreadsheet compu ...
- 自学Aruba5.1.2-带宽限制
点击返回:自学Aruba之路 自学Aruba5.1.2-带宽限制 1 针对role --可以限制所有数据 注:带宽限制需要PEFNG许可证 单位可以是kbits或是mbits 可以是上传(up ...