当前位置:网站首页>实验4 循环程序设计
实验4 循环程序设计
2022-04-22 06:06:00 【Lupin123123】
循环程序设计
1.实验目的
(1)理解循环结束条件、如何控制循环次数;
(2)掌握while循环的设计方法;
(3)掌握for语句单层循环和双重循环结构;
(4)能够正确利用循环解决复杂计算问题。
2.实验任务
(1)用for语句遍历列表 [1,2,3],并打印列表元素。
for i in [1,2,3]:
print("循环第%d次"%(i))
循环第1次
循环第2次
循环第3次
(2)for 循环后面可以加入else,在循环结束后(正常结束)会执行
for i in [1,2,3]:
print("循环第%d次"%(i))
else:
print("循环完毕")
循环第1次
循环第2次
循环第3次
循环完毕
for i in [1,2,3]:
print("循环第%d次"%(i))
if i>= 2: break
else:
print("循环完毕")
循环第1次
循环第2次
(3)while循环
当满足条件时循环执行语句块,想要结束循环时,使用break或continue结束循环。
i = 0 # 新建i变量
while i<9: # 设置循环条件
i+=1 # 每次循环i增加1
if i == 3: # 判断条件是否满足
print("跳出此次循环")
continue # continue跳出当前的这一次循环
if i == 5:
print("跳出当前大的循环")
break # 跳出当前的大的循环
print(i)
1
2
跳出此次循环
4
跳出当前大的循环
(4)自行编码,求1!+2!+3!+… + N!,其中N由键盘输入。
N = int(input("N="))
ans = 0
fac = 1
for i in range(1, N+1):
fac *= i
ans += fac
print(ans)
4037913
(5)编码打印所有“水仙花数”。
所谓水仙花数,指一个三位数,各位数字的立方和等于该数本身。
例如, 153 = 1 3 + 5 3 + 3 3 153=1^3+5^3+3^3 153=13+53+33,所以153是一个水仙花数。
for i in range(100, 1000):
bai = i//100
shi = i//10%10
ge = i%10
# print(bai, shi, ge)
if ((bai*bai*bai+shi*shi*shi+ge*ge*ge)==i):
print(i)
153
370
371
407
4.分析与讨论
(1)break和continue在循环语句中的作用有何不同?
ans: break指的是跳出循环,continue指的是跳过这一次循环
(2)编码输出Fibonacci数列,直到数值超过1000为止。
该数列形式为 1,1,2,3,5,8…
a = [0 ,1, 1]
cnt = 1
while 1:
if (cnt>=3):
a.append(a[cnt-1]+a[cnt-2])
cnt+=1
if (a[cnt-1]<=1000):
print(a[cnt-1])
else:
break
else:
print(a[cnt])
cnt+=1
#如果喜欢数学的同学可以思考,如何更快速地求出fib数列的第n项
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
版权声明
本文为[Lupin123123]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_28972011/article/details/124185411
边栏推荐
- Understand 16S sequencing of microbial amplicons
- 一阶数字低通滤波器-C语言/matlab实现
- 怎么能改善高速信号的通道损耗
- 知识图谱综述(三)
- Custom notification reference article
- Using keras framework to write three-layer neural network to solve the problem of house price prediction
- PCB标记的埋雷设计,短路了却找不到一丝踪迹
- 如何调节肠道菌群?常见天然物质、益生菌、益生元的介绍
- ROS系列(二):ROS快速体验,以HelloWorld程序为例
- How to discover and learn the story hidden behind the published metagenome articles
猜你喜欢

OpenCV4二维码识别测试

【AI视野·今日Sound 声学论文速览 第一期】Thu, 14 Apr 2022

肠道菌群失调是II型糖尿病和炎症性肠病的标志物

Clark transform of PMSM FOC control MATLAB / Simulink simulation

测试姿势要严谨

解决seq2seq+attention机器翻译中的技术小难题

Glide conflicts with picture selector Matisse after 4.0.0

ARM上安装PaddlePaddle

肠道核心菌属——毛螺菌属(Lachnospira)

PMSM FOC控制 Matlab/Simulink仿真之Clark变换
随机推荐
Solution: attributeerror: module 'yaml' has no attribute 'csafeloader‘
如何在CMake项目中引入OpenCV
关于Mask-Rcnn中标注工具VIA(VGG Image Annotator)使用的详解
STM32 clock division Tim_ Detailed description of clockdivision configuration and use
ArcGIS performs horizon analysis based on tin ground surface data and building data
Install Matplotlib and bug resolution in the specified environment
让MATLAB2018b支持VS2019的mex配置
Understand 16S sequencing of microbial amplicons
PMSM FOC控制 Matlab/Simulink仿真之反Clark变换
示波器只能测波形-格局小啦
Custom notification reference article
STM32 TIM 定时器 OCREF 输出配置 TIMx->CCER
卷积神经网络基础知识
MDK scope joint debugging RTT mode multi-channel
ROS系列(四):ROS通信机制系列(3):参数服务器
Nature Medicine 揭示冠状动脉疾病的个体危险因素
测试姿势要严谨
计数排序(C语言实现)------学习笔记
很难相信,这对高速信号换了那么多次过孔!!!
c#窗体设计 鼠标靠近 控件显示 提示 备注 信息