博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python语言之控制流(if...elif...else,while,for,break,continue)
阅读量:6913 次
发布时间:2019-06-27

本文共 1473 字,大约阅读时间需要 4 分钟。

1.if...elif...else...

1 number = 23 2 guess = int(input('Enter an integer : ')) 3  4 if guess == number: 5     print( 'Congratulations, you guessed it.' ) # New block starts here 6     print( "(but you do not win any prizes!)" ) # New block ends here 7 elif guess < number: 8     print( 'No, it is a little higher than that' )# Another block 9 else:10     print( 'No, it is a little lower than that' )11 12 print( 'Done' )13 # This last statement is always executed, after the if statement is executed

 

 

2.while

1 number = 23 2 running = True 3  4 while running: 5     guess = int(input('Enter an integer : ')) 6  7     if guess == number: 8         print( 'Congratulations, you guessed it.' ) 9         running = False # this causes the while loop to stop10     elif guess < number:11         print( 'No, it is a little higher than that' )12     else:13         print( 'No, it is a little lower than that' )14 else:15     print( 'The while loop is over.' )16     # Do anything else you want to do here17 18 print( 'Done' )

while循环条件变为False时,else块被执行。

你可能想问,这样的else块始终会被执行到,还要else干嘛。别着急,在4.break中有解答。

3.for

1 for i in range(1, 5):2     print( i )3 else:4     print ( 'The for loop is over' )

range(n, m [,stepLen]) = [n,n+stepLen,n+stepLen*2,...,n+(ceil((m-n)/stepLen)-1)*stepLen]

即从n开始,以stepLen为步长,一直到小于m的序列(不包括m)。默认情况下stepLen=1。

4.break

终止当前循环语句。

注意如果从for或while循环终止,任何对应的else将不会被执行。

5.continue

跳过当前循环块中的剩余语句,然后继续进行下一轮的循环。

转载于:https://www.cnblogs.com/magnolia/p/4056471.html

你可能感兴趣的文章
096实战 在windows下新建maven项目
查看>>
阿里云产品介绍(一):云服务器ECS
查看>>
linux设置系统时间
查看>>
班级里将来有成就的学生往往都是那些成绩中等的学生
查看>>
php iframe 上传文件
查看>>
ES6的Generator函数
查看>>
dockerfile 介绍
查看>>
通过nginx搭建hls流媒体服务器
查看>>
java--------抽象类与接口的区别
查看>>
vue 目录结构与文件配置说明
查看>>
单点登录CAS-Demo
查看>>
物联网数据卡系统源码——物联网技术架构图
查看>>
Linux内存使用情况以及内存泄露分析之工具与方法
查看>>
安装 Ruby, Rails 运行环境
查看>>
Office EXCEL 如何保留一位小数,并且单击这个单元格的时候没有一大串小数
查看>>
Unity ShaderLab学习总结
查看>>
JS 遍历JSON中每个key值
查看>>
一些小功能实现
查看>>
前端学习 -- Css -- 行间距
查看>>
android图像处理(3) 浮雕效果
查看>>