Python 3基础教程 第3章程序流程控制
- 格式:ppt
- 大小:679.00 KB
- 文档页数:53
python程序的流程控制习题下载温馨提示:该文档是我店铺精心编制而成,希望大家下载以后,能够帮助大家解决实际的问题。
文档下载后可定制随意修改,请根据实际需要进行相应的调整和使用,谢谢!并且,本店铺为大家提供各种各样类型的实用资料,如教育随笔、日记赏析、句子摘抄、古诗大全、经典美文、话题作文、工作总结、词语解析、文案摘录、其他资料等等,如想了解不同资料格式和写法,敬请关注!Download tips: This document is carefully compiled by theeditor.I hope that after you download them,they can help yousolve practical problems. The document can be customized andmodified after downloading,please adjust and use it according toactual needs, thank you!In addition, our shop provides you with various types ofpractical materials,such as educational essays, diaryappreciation,sentence excerpts,ancient poems,classic articles,topic composition,work summary,word parsing,copy excerpts,other materials and so on,want to know different data formats andwriting methods,please pay attention!Python程序的流程控制:探索与实践在编程的世界中,流程控制是至关重要的概念,它决定了程序的执行顺序和逻辑。
一.实验目的1.熟悉顺序结构;2.掌握选择结构:if语句的使用3.掌握循环结构:for语句、while语句的使用;二.实验内容与步骤1、编写程序,计算1+3+5+7…+99之和。
2、编写程序,使用不同的实现方法输出2000~3000的所有闰年。
3、编写程序,计算S n=1+1/2+1/3+…。
4、编写程序,输入三角形的3条边,先判断是否可以构成三角形,如果可以,则进一步求三角形的周长和面积,否则报错“无法构成三角形!”。
程序代码:1、# 编写程序,计算1+3+5+7+。
+99之和sum = 0for i in range(1, 100, 2):sum += iprint(sum)2、# 编写程序,使用不同的实现方法输出2000~3000的所有闰年.for i in range(2000, 3001):if(i % 400 == 0) or (i % 4 == 0 and i % 100 != 0):print(i,end = " ")3、# 编写程序,计算Sn=1+1/2+1/3+…。
sum = 0n = eval(input('请输入一个数字:'))for i in range(1, n + 1):sum += 1 / iprint(sum)4、# 编写程序,输入三角形的3条边,先判断是否可以构成三角形,如果可以,则进一步求三角形的周长和面积,否则报错“无法构成三角形!”。
import matha = eval(input('请输入三角形的第一条边:'))b = eval(input('请输入三角形的第二条边:'))c = eval(input('请输入三角形的第三条边:'))h = (a + b + c) / 2if a + b > c and a + c > b and b + c >a :print("三角形的周长为:{:},三角形的面积为:{:.2f}".format(\a +b + c, math.sqrt(h*(h-a)*(h-b)*(h-c))))else:print("无法构成三角形")实验结果:1、2、3、4、。