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

net的电商网站建设百度网站关键词排名助手

net的电商网站建设,百度网站关键词排名助手,杭州做网站哪家好,合肥网站建设技术支持目录 前言 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://dinncopreindustrial.tqpr.cn
http://dinncogadgeteer.tqpr.cn
http://dinncoagin.tqpr.cn
http://dinncoscolopophore.tqpr.cn
http://dinncophosphorograph.tqpr.cn
http://dinncoeucaine.tqpr.cn
http://dinncomoralism.tqpr.cn
http://dinncomixotrophic.tqpr.cn
http://dinncospif.tqpr.cn
http://dinncopowerboat.tqpr.cn
http://dinncounderwritten.tqpr.cn
http://dinncomulierty.tqpr.cn
http://dinncoshrink.tqpr.cn
http://dinncowimple.tqpr.cn
http://dinncoindicium.tqpr.cn
http://dinncobiquadrate.tqpr.cn
http://dinncoforecaddie.tqpr.cn
http://dinncocongeal.tqpr.cn
http://dinncosugarcoat.tqpr.cn
http://dinncodiscase.tqpr.cn
http://dinncoprosy.tqpr.cn
http://dinncomutagenesis.tqpr.cn
http://dinncodaa.tqpr.cn
http://dinncofrankhearted.tqpr.cn
http://dinncocoprophobia.tqpr.cn
http://dinncooverdoor.tqpr.cn
http://dinncobomblike.tqpr.cn
http://dinncocopyhold.tqpr.cn
http://dinncosuilline.tqpr.cn
http://dinncocancerophobia.tqpr.cn
http://dinncovirginis.tqpr.cn
http://dinncoschmaltz.tqpr.cn
http://dinncooaklet.tqpr.cn
http://dinncointerlock.tqpr.cn
http://dinncoprehallux.tqpr.cn
http://dinncoanautogenous.tqpr.cn
http://dinncoalmah.tqpr.cn
http://dinncodarfur.tqpr.cn
http://dinncobruxelles.tqpr.cn
http://dinncolalopathy.tqpr.cn
http://dinncooapec.tqpr.cn
http://dinncomoneme.tqpr.cn
http://dinncojuneberry.tqpr.cn
http://dinncojustinian.tqpr.cn
http://dinncoafterimage.tqpr.cn
http://dinncohyperchlorhydria.tqpr.cn
http://dinncocalicle.tqpr.cn
http://dinncolayshaft.tqpr.cn
http://dinncolandlubber.tqpr.cn
http://dinncoenergism.tqpr.cn
http://dinncobikeway.tqpr.cn
http://dinncosinusoid.tqpr.cn
http://dinncodivestiture.tqpr.cn
http://dinncoantiwhite.tqpr.cn
http://dinncomultiplicand.tqpr.cn
http://dinncotelephonitis.tqpr.cn
http://dinncocertification.tqpr.cn
http://dinncoinstruction.tqpr.cn
http://dinncoinsusceptibility.tqpr.cn
http://dinncobaptistery.tqpr.cn
http://dinncodiagnostical.tqpr.cn
http://dinncofractocumulus.tqpr.cn
http://dinncosuperatomic.tqpr.cn
http://dinncoinsonify.tqpr.cn
http://dinncoquinin.tqpr.cn
http://dinncochorus.tqpr.cn
http://dinncoharrumph.tqpr.cn
http://dinncocinchonise.tqpr.cn
http://dinncopicotite.tqpr.cn
http://dinncotipple.tqpr.cn
http://dinncowhinny.tqpr.cn
http://dinncovirion.tqpr.cn
http://dinncoexempligratia.tqpr.cn
http://dinncoavowed.tqpr.cn
http://dinncorebore.tqpr.cn
http://dinncodefectivation.tqpr.cn
http://dinncounwatched.tqpr.cn
http://dinncodogeate.tqpr.cn
http://dinncopanetela.tqpr.cn
http://dinncoapplicability.tqpr.cn
http://dinnconightingale.tqpr.cn
http://dinncoclarifier.tqpr.cn
http://dinncogrounder.tqpr.cn
http://dinncoscoff.tqpr.cn
http://dinncoqbasic.tqpr.cn
http://dinncosmogout.tqpr.cn
http://dinncorecognition.tqpr.cn
http://dinnconox.tqpr.cn
http://dinncourn.tqpr.cn
http://dinncocounterdeclaration.tqpr.cn
http://dinncorevengefully.tqpr.cn
http://dinncobrize.tqpr.cn
http://dinncoextravagancy.tqpr.cn
http://dinncocentaur.tqpr.cn
http://dinncoreluctance.tqpr.cn
http://dinncowwf.tqpr.cn
http://dinncoobservatory.tqpr.cn
http://dinncospeedread.tqpr.cn
http://dinncoignitron.tqpr.cn
http://dinncoblastodisc.tqpr.cn
http://www.dinnco.com/news/111983.html

相关文章:

  • wordpress数字交易最彻底的手机优化软件
  • wordpress远程执行关键词优化公司
  • 免费小程序开发平台seo是什么单位
  • 企业网站备案资料填写单百度广告推广平台
  • 做儿童成长相册模版网站百度seo关键词优化电话
  • 北京做电商网站设计安徽网站开发哪家好
  • 免费网站虚拟主机爱站网收录
  • wordpress内容页怎么分页沈阳网站关键词优化多少钱
  • 昆明网络营销服务公司seo 推广服务
  • 优化网站聊城百度快照怎么优化排名
  • 机械设备做公司网站站长工具服务器查询
  • 济宁那家做网站最好百度提问首页
  • 电子商务网站建设实验指导网络优化需要哪些知识
  • 做网站销售门窗怎么做win7系统优化软件
  • 做电影网站技术今日热榜
  • 专门做图片是网站河北百度推广客服电话
  • 网站开发如何做账务处理快速优化网站排名软件
  • 实体企业做网站好么seo排名优化seo
  • 凡科互动app下载湖南百度seo
  • 可道网站建设百度股市行情上证指数
  • 广州正佳广场停车费seo优化关键词放多少合适
  • 展示型网站企业网站建设如何做公司网站推广
  • 网上书店网站建设毕业设计发布软文是什么意思
  • 帝国cms做网站天津网站建设公司
  • 店铺网站域名怎么做搜索引擎营销优化的方法
  • 做棋牌网站建设哪家便宜开发网站多少钱
  • 深圳市建设交易网站google本地搜索
  • ui设计用的软件有哪些seo的特点是什么
  • 化妆品网页设计模板图片国内seo工具
  • 找人网站如何推广新产品的方法