对python while循环和双重循环的实例详解
作者:tea_year 时间:2021-01-17 22:22:59
废话不多说,直接上代码吧!
#python中,while语句用于循环执行程序,即在某个条件下,循环执行某段程序,以处理需要重复处理的相同任务。
#while是“当型”循环结构。
i=1
while i<=20:
print(i,end=" ")
i+=1
sum=0
i=1
while i<=100:
sum+=i
i+=1
else:
print("\n",sum)
print("0+2+...+100=",sum)
#从控制台录入一个数字,求从1到数字的阶乘;
result=1;i=1;numA=0;
numA=int(input("请输入一个数字:"))
while i<=numA:
result*=i
i+=1
else:
print("循环结束!")
print("结果是:",result)
#使用while循环输出100以内的素数
x=2
while x<100:
n=2
while n<x-1:
if x%n==0:
break
n+=1
else:
print(x,end=" ")
x+=1
else:
print("循环结束")
#输出100之内能被7整除的数
nNum=7;lineCount=0;
while nNum<=100:
if nNum%7 !=0:
nNum+=1;
continue
else:
print(nNum,end=" ")
lineCount+=1
if lineCount==5:
print() #单个print()换行;
lineCount=0
nNum+=1;
else:
print("\n循环结束")
#使用while循环计算从1到100之和
sum=0;i=1;
while i<=100:
sum+=i;
i+=1;
else:
print("1+2+3+...+100=",sum);
print("\n程序结束")
#从控制台录入打印*符号的数量。
row=0;i=0;
row=int(input("请输入行数:"));
while i<row:
print()
print("*",end=" ")
i+=1;
else:
print("\n程序运行结束")
#使用while循环和for循环嵌套.python语言允许在一个循环体中嵌套另外一个循环体。
#实例:从控制台输入行数和列数,打印正方形。
i=1;j=1;
numA=int(input("请输入行数:"))
numB=int(input("请输入列数:"))
while i<=numA:
j=1
while j<=numB:
print("*",end=" ")
j+=1
print()
i+=1
#使用while循环嵌套打印九九乘法口诀表;
i=1;j=1;
while i<=9:
j=1
while j<=i:
print("%d*%d=%2d"%(j,i,i*j),end=" ")
j+=1
print()
i+=1
else:
print("\n程序运行结束")
#使用for循环打印九九乘法口诀表;
for i in range(1,10):
for j in range(1,10):
if j<=i:
print("%d*%d=%2d"%(j,i,i*j),end=" ")
else:
print()
else:
print("\n程序运行结束");
来源:https://blog.csdn.net/zhangchen124/article/details/81773264
标签:python,while,循环,双重
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python 实现敏感词过滤的方法
2022-08-09 10:22:51
python matplotlib 画dataframe的时间序列图实例
2023-05-17 00:12:34
Python爬虫之爬取最新更新的小说网站
2022-04-01 17:11:07
![](https://img.aspxhome.com/file/2023/8/68868_0s.png)
基于Python的Houdini插件开发过程详情
2023-11-28 17:28:02
![](https://img.aspxhome.com/file/2023/7/72167_0s.png)
Python使用PIL模块生成随机验证码
2022-03-05 21:54:11
Pycharm新建模板默认添加个人信息的实例
2022-03-30 20:59:50
![](https://img.aspxhome.com/file/2023/5/80585_0s.jpg)
python入门for循环嵌套理解学习
2021-03-01 21:42:16
![](https://img.aspxhome.com/file/2023/8/71388_0s.png)
15个短代码示例理解python丰富的编程思维
2022-07-18 08:16:57
淘宝2011新版首页开发实践
2011-01-20 20:07:00
![](https://img.aspxhome.com/file/UploadPic/20111/20/ued-blog-wpo-1-78s.png)
利用Python如何画一颗心、小人发射爱心
2021-01-11 09:11:50
![](https://img.aspxhome.com/file/2023/7/95887_0s.png)
numpy工程实践之np.savetxt()存储数据
2023-06-19 07:33:11
![](https://img.aspxhome.com/file/2023/5/59655_0s.png)
利用Python实现读取Word文档里的Excel附件
2022-01-21 11:28:18
![](https://img.aspxhome.com/file/2023/8/75158_0s.jpg)
python中的decimal类型转换实例详解
2022-05-01 15:05:21
python中Scrapy shell的使用
2021-03-23 08:23:24
![](https://img.aspxhome.com/file/2023/1/94111_0s.png)
url传递中文的解决方案
2007-10-09 20:17:00
Python使用进程Process模块管理资源
2023-08-08 07:08:01
SQL Server数据库日志清除的两个方法
2009-01-08 13:44:00
如何绝对获知浏览器类型?
2009-12-16 18:58:00
对python指数、幂数拟合curve_fit详解
2022-12-03 16:28:52
![](https://img.aspxhome.com/file/2023/9/95269_0s.jpg)
Python opencv应用实现图片切分操作示例
2021-12-25 03:45:39
![](https://img.aspxhome.com/file/2023/6/72876_0s.png)