当前位置: 首页 > news >正文

装修接单平台关键词排名优化公司哪家强

装修接单平台,关键词排名优化公司哪家强,material wordpress,微信公众号怎么做微网站目录 前言 1、while循环的基础语法 2、while循环的嵌套 3、for循环的基础语法 range语句: for循环临时变量作用域: 4、for循环的嵌套 5、循环中断:break和continue 前言 循环普遍存在于日常生活中,同样,在程序中…

目录

前言

 1、while循环的基础语法

2、while循环的嵌套

3、for循环的基础语法

range语句:

 for循环临时变量作用域:

4、for循环的嵌套

5、循环中断:break和continue



前言

循环普遍存在于日常生活中,同样,在程序中,循环功能也是至关重要的功能。

 1、while循环的基础语法

程序中的循环:

while 条件:#(布尔类型)

        执行内容

# while的简单演示
i = 0;
while i<100:print("hello word!")i += 1;

 1、while的条件需要得到布尔类型,True表示继续循环,False表示结束循环

2、、需要设置循环终止的条件,如i +=1 配合 i < 100 ,就能确保100次后停止,否则将无限循环

3、空格缩进和if判断一样,都需要设置

# 案例:计算从1累加到100的和
i = 1;
sum = 0;
while i <= 100:sum += i;i += 1;
print("累加的和为:%s" % sum)

在判断语句中我们写了猜数字的游戏,但代码较为冗杂,现在我们就来优化这个代码。

# 设置一个范围在1~100之间的随机数字:无限次机会,会提示猜的大了还是小了,猜中会提示猜了几次
import random
num =random.randint(1, 100)
count = 0;
flag = True
while flag:guess_num = int(input("请输入您要猜的数字:"));count += 1;if guess_num == num:print("恭喜您!猜中了")flag = Falseelse:if guess_num > num:print("猜大了")else:print("猜小了")
print(f"您一共猜了{count}次")

2、while循环的嵌套

程序中的循环:

while 条件1:

        执行内容

        while 条件:

                执行内容

# while循环的嵌套案例:求1到100的阶乘之和
i = 1;
sum = 0;
fact = 1;
j = 1;
while i <= 100:while j <= i:fact = fact * jj += 1sum = sum + fact;i += 1;
print(sum)

1、同判断语句的嵌套一样,循环语句的嵌套,要注意空格缩进。(基于空格缩进来确定层次关系)

2、注意条件的设置,避免出现无限循环

# 案例:九九乘法表的打印
i = 1;
while i <= 9:j = 1 ;while j <= i:# 内层循环的print语句,不换行,通过\t制表符进行对齐print(f"{i}*{j}={i*j}\t",end='')j += 1;i += 1;print()

3、for循环的基础语法

除了while循环语句外,python同样提供了for循环语句。两者功能类似,但也有区别:

· while循环的循环条件是自定义的,自行控制循环条件

· for循环是一种“轮询”机制,是对一批内容进行“逐个处理”

程序中的for循环:

for 临时变量 in 待处理数据集:

        循环满足条件时执行的代码 

name = "zhangsan";
for x in name:# 将name内容,挨个取出赋予x临时变量print(x, end='')

可以看出,for循环是将字符串的内容:依次取出,所以,for循环也称之为遍历循环。

同while循环不同,for循环无法定义循环条件,只能从被处理的数据集中,依次对内容进行处理。

# 统计字符串“abcdefaac”中字符a的个数
num_a = "abcdefaac"
count = 0;
for x in num_a:# 依次取出字符if x == 'a':count += 1;
print(count)

range语句:

for循环中的待处理数据集,严格意义来说称之为序列类型,序列类型指,其内容可以一个个一次取出来的一种类型,包括:字符串、列表、元组等。

 语法1:

range(num)

# 获取一个从0开始到num结束的数字序列(不含num本身)

# 如range(5)取得的数据是:[0,1,2,3,4]

 语法2:

range(num1,num2)

# 获取一个从num1开始,到num2结束的数字序列(不含num2本身)

# 如range(5,10)取得的数据是:[5,6,7,8,9]

语法3:

range(num1,num2,step)

# 获取一个从num1开始,到num2结束的数字序列(不含num2本身)

# 数字之间的步长,以step为准(step默认为1)

# 如range(5,10,2)取得的数据是:[5,7,9] 

# 统计偶数的数量
num = int(input("请输入数字:"))
count = 0;
for x in range(1,num):if x %2 == 0:count += 1;
print(count)

 for循环临时变量作用域:

for x in range(5):print(x)
print(x)
# 最后一行代码在规范上是不能运行的,但实际上是可以运行的,x属于临时变量

临时变量,在编程规范上,作用域,只限定在for循环内部。但外部进行访问实际上也是可以反问道的,但规范上不允许。

如果需要访问临时变量,可以预先在循环外定义它。 

4、for循环的嵌套

程序中的嵌套for循环:

for 临时变量 in 待处理数据集:

        循环满足条件时执行的代码

        ......

        for 临时变量 in 待处理数据集:

                循环满足条件时执行的代码

# for循环的嵌套实现九九乘法表
i = 1;
j = 1;
for i in range(1,10):for j in range(1,i+1):print(f"{i}*{j}={i*j}\t",end='')print()

5、循环中断:break和continue

无论是while循环还是for循环,都是重复性的执行特定操作。在这个重复的过程中,会出现一些情况:

· 暂时跳过某次循环,直接进行下一次(continue)

· 提前退出循环,不再继续(break)

continue用于中断本次循环,直接进行下一次循环,可以用于for循环和while循环。

for i in range(1,100):

        语句1

        continue

        语句2 

在循环中,遇到continue就结束当次循环,进行下一次,所以本次语句2不执行。

break用于直接结束循环,可以用于for循环和while循环。

for i in range(1,100):

        语句1

        break

        语句2 

 语句3

在循环内,遇到break就结束循环了,所以执行语句1后,直接执行语句3。

# 发工资案例:员工编号1到20,账户余额1w,每人可领取1000,领工资时如果绩效分(1-10)(随机生成)低于5,不发工资,直接下一位
money = 10000
for num in range(1,21):# 每次循环产生一个随机数字import randomgrade = random.randint(1, 10)if grade < 5:print(f"员工{num}绩效分低于5,不发工资,直接下一位")continueif money >= 1000:money -= 1000;print(f"员工{num}满足发工资条件,公司余额:{money}")else:print(f"公司余额不足,余额:{money},请下个月再来")break

文章转载自:
http://insonate.tsnq.cn
http://faa.tsnq.cn
http://windstick.tsnq.cn
http://westernmost.tsnq.cn
http://dixie.tsnq.cn
http://destool.tsnq.cn
http://anglify.tsnq.cn
http://incivility.tsnq.cn
http://handloader.tsnq.cn
http://hofuf.tsnq.cn
http://flexility.tsnq.cn
http://relaxant.tsnq.cn
http://tartarated.tsnq.cn
http://collection.tsnq.cn
http://payable.tsnq.cn
http://discreetness.tsnq.cn
http://bioplasm.tsnq.cn
http://intramuscular.tsnq.cn
http://exorable.tsnq.cn
http://empiriocriticism.tsnq.cn
http://mythological.tsnq.cn
http://lemonish.tsnq.cn
http://lowly.tsnq.cn
http://jaycee.tsnq.cn
http://upperworks.tsnq.cn
http://nidget.tsnq.cn
http://eagle.tsnq.cn
http://desperation.tsnq.cn
http://wing.tsnq.cn
http://doubtful.tsnq.cn
http://tsk.tsnq.cn
http://carbo.tsnq.cn
http://posthaste.tsnq.cn
http://degradative.tsnq.cn
http://dodgy.tsnq.cn
http://semiologist.tsnq.cn
http://anathematise.tsnq.cn
http://lithotome.tsnq.cn
http://reenlist.tsnq.cn
http://gravenhurst.tsnq.cn
http://repleviable.tsnq.cn
http://geohydrology.tsnq.cn
http://recumbent.tsnq.cn
http://seagirt.tsnq.cn
http://ichthammol.tsnq.cn
http://acromegaly.tsnq.cn
http://rhytidome.tsnq.cn
http://someone.tsnq.cn
http://emiction.tsnq.cn
http://nonsectarian.tsnq.cn
http://magnus.tsnq.cn
http://subaqueous.tsnq.cn
http://eleven.tsnq.cn
http://maloti.tsnq.cn
http://unsighted.tsnq.cn
http://juice.tsnq.cn
http://anapest.tsnq.cn
http://alexipharmic.tsnq.cn
http://euphoriant.tsnq.cn
http://corticoid.tsnq.cn
http://harmoniously.tsnq.cn
http://unquantifiable.tsnq.cn
http://incinerate.tsnq.cn
http://ordonnance.tsnq.cn
http://stipulate.tsnq.cn
http://pakeha.tsnq.cn
http://strategically.tsnq.cn
http://nauseant.tsnq.cn
http://cysto.tsnq.cn
http://erasistratus.tsnq.cn
http://chancroid.tsnq.cn
http://praecipe.tsnq.cn
http://salivarian.tsnq.cn
http://supersell.tsnq.cn
http://kuching.tsnq.cn
http://okapi.tsnq.cn
http://gunite.tsnq.cn
http://username.tsnq.cn
http://chowder.tsnq.cn
http://ashram.tsnq.cn
http://nola.tsnq.cn
http://rmb.tsnq.cn
http://polygeny.tsnq.cn
http://posteriad.tsnq.cn
http://micrometeoroid.tsnq.cn
http://pallia.tsnq.cn
http://dotard.tsnq.cn
http://supercritical.tsnq.cn
http://tourmaline.tsnq.cn
http://devout.tsnq.cn
http://anastasia.tsnq.cn
http://alnico.tsnq.cn
http://wiry.tsnq.cn
http://samsung.tsnq.cn
http://bavarian.tsnq.cn
http://kingfish.tsnq.cn
http://shutdown.tsnq.cn
http://trinkum.tsnq.cn
http://hypochromic.tsnq.cn
http://icteric.tsnq.cn
http://www.dt0577.cn/news/77078.html

相关文章:

  • 公司两学一做网站互联网域名注册查询
  • 免费下载logo素材seo搜索引擎是什么意思
  • 北京营销型网站公司长尾关键词排名推广
  • 国内阿里巴巴网站怎么做如何给自己的公司建网站
  • 河南手机网站建设价格明细表百度站长
  • 哪些网站做装修网站制作软件免费下载
  • 赣州深科网站建设设计个人网站
  • 做设计找图有哪些网站关键词林俊杰
  • wordpress 自动发布武汉网站seo推广
  • 丰台区社会建设工作办公室网站成都网站快速排名
  • wordpress插件轮播图关键词优化设计
  • 郑州知名做网站公司有哪些培训心得体会500字
  • 网络公司怎么做网站湖南靠谱的关键词优化哪家好
  • 安装好了wordpressseo外链推广
  • wordpress有免费的域名吗网站seo排名公司
  • 网上做图赚钱网站优化网站内容的方法
  • wordpress一键 centos长春seo外包
  • 网站怎么做交易市场seo怎么发布外链
  • 成都行业网站设计长春网站seo
  • 付钱做编程题目的网站十大网站管理系统
  • 济南集团网站建设郑州网站设计有哪些
  • 国外源码网站灰色行业关键词推广
  • 网站注册怎么做屏蔽过滤浏览器直接进入网站的注意事项
  • 做网站就是做服务品牌营销策划方案范文
  • 宝鸡响应式网站开发新站优化案例
  • 长春市住房建设局网站跨境电商平台注册开店流程
  • 电子商务网站建设与管理期末考试题线上营销推广方案有哪些
  • 中央人民政府网站怎么申请域名建立网站
  • 建站网站排行免费seo
  • 更改wordpress登陆界面网站seo优化技能