【20171001】python_语言设计(2)循环与分支
来源:互联网 发布:巅峰减重 知乎 编辑:程序博客网 时间:2024/06/05 08:47
1.简单分支
e.g.PM2.5指数分级程序结构分级结构
#pm25.pydef main() PM=eval(input("what is today's PM2.5?")) if PM>75: print("unhealthy!be careful!") if PM<35: print("good!go running!")main()
条件满足执行,条件不满足则跳过,无论如何都跳到下一句。
关系操作符:<,<=,==,>=,>,!=(数字,字符串)
#计算二次方程的实数根程序,如果没有实数根则报错import mathdef main(): print("this program finds the real solutions to a quadratic\n") a,b,c=eval(input("please enter the coefficients(a,b,c):")) discRoot=math.sqrt(b*b-4*a*c) root1=(-b+discRoot)/(2*a) root2=(-b-discRoot)/(2*a) print("\nthe solutions are: ",root1,root2)main()
改进后程序
#计算二次方程的实数根程序import mathdef main(): print("this program finds the real solutions to a quadratic\n") a,b,c=eval(input("please enter the coefficients(a,b,c):")) delta=b*b-4*a*c if delta>=0: discRoot=math.sqrt(b*b-4*a*c) root1=(-b+discRoot)/(2*a) root2=(-b-discRoot)/(2*a) print("\nthe solutions are: ",root1,root2)main()
2.二分支结构
if else结构
#计算二次方程的实数根程序import mathdef main(): print("this program finds the real solutions to a quadratic\n") a,b,c=eval(input("please enter the coefficients(a,b,c):")) delta=b*b-4*a*c if delta<0: print("the equation has no real roots!") else: discRoot=math.sqrt(b*b-4*a*c) root1=(-b+discRoot)/(2*a) root2=(-b-discRoot)/(2*a) print("\nthe solutions are: ",root1,root2)main()
3.多分支结构
解决复杂问题,三分支结构可以由两个二分支机构。
if-elif-else结构
#计算二次方程的实数根程序import mathdef main(): print("this program finds the real solutions to a quadratic\n") a,b,c=eval(input("please enter the coefficients(a,b,c):")) delta=b*b-4*a*c if delta<0: print("\nthe equation has no real roots!") elif delta==0: discRoot=math.sqrt(b*b-4*a*c) root1=(-b+discRoot)/(2*a) print("\nsingleroot is:",root1) else: discRoot=math.sqrt(b*b-4*a*c) root1=(-b+discRoot)/(2*a) root2=(-b-discRoot)/(2*a) print("\nthe solutions are: ",root1,root2)main()
改进:考虑a=0时退化为一次问题求解
#计算二次方程的实数根程序import mathdef main(): print("this program finds the real solutions to a quadratic\n") a,b,c=eval(input("please enter the coefficients(a,b,c):")) delta=b*b-4*a*c if a==0: x=-b/c print("\nsolution is:",x) elif delta<0: print("\nthe equation has no real roots!") elif delta==0: discRoot=math.sqrt(b*b-4*a*c) root1=(-b+discRoot)/(2*a) print("\nsingleroot is:",root1) else: discRoot=math.sqrt(b*b-4*a*c) root1=(-b+discRoot)/(2*a) root2=(-b-discRoot)/(2*a) print("\nthe solutions are: ",root1,root2)main()
#空气质量提醒程序#newpm25.pydef main(): PM=eval(input("what is today's pm2.5?")) if PM<35: print("good!") elif PM<75: print("moderate!") elif PM<115: print("unhealthy!") elif PM<150: print("limit!") elif PM<250: print("very unhealthy!") else: print("hazadous!")main()
4.异常处理
异常处理机制(程序),try…except…清理工作场合
else–必须在finally之前,finally–必须最后,关键字
def main(): try: number1,number2=eval(input("enter two numbers,seperated by a comma:")) result=number1/number2 except ZeroDivisionError: print("division by zero!") except SyntaxError: print("A comma may be missing in the input") except: print("something wrong in the input") else: print("No exceptions,the result is",result) finally: print("executing the final clause")main()
#计算二次方程的实数根程序,如果没有实数根则报错import mathdef main(): print("this program finds the real solutions to a quadratic\n") try: a,b,c=eval(input("please enter the coefficients(a,b,c):")) discRoot=math.sqrt(b*b-4*a*c) root1=(-b+discRoot)/(2*a) root2=(-b+discRoot)/(2*a) print("\nthe solutions are: ",root1,root2) except ValueError: print("\nNo real roots")main()
捕捉任何类型的错误,NameError,SyntaxError…
改进二次求根错误类型
#计算二次方程的实数根程序,如果没有实数根则报错import mathdef main(): print("this program finds the real solutions to a quadratic\n") try: a,b,c=eval(input("please enter the coefficients(a,b,c):")) discRoot=math.sqrt(b*b-4*a*c) root1=(-b+discRoot)/(2*a) root2=(-b+discRoot)/(2*a) print("\nthe solutions are: ",root1,root2) except ValueError as excObj: if str(excObj)=="math domain error": print("No Real Roots.") else: print("You didn't give me the right number of coefficients.") except NameError: print("\nYou did't enter three numbers.") except TypeError: print("\nYour inputs were not all numbers.") except SyntaxError: print("\nYour input was not in the correct form.Missing comma?") except: print("\nSomething went wrong,sorry!") #print("\nNo real roots")main()
5.三者最大实例
#三者最大值#1.通盘比较,分支结构def main(): x1,x2,x3=eval(input("请输入三个值:")) if x1>=x2 and x1>=x3: max=x1 elif x2>=x1 and x2>=x3: max=x2 else: max=x3 print("max为:",max)main()
#三者最大值#2.决策树方法避免冗余比较def main(): x1,x2,x3=eval(input("请输入三个值:")) if x1>=x2: if x1>=x3: max=x1 else max=x3 else: if x2>=x3: max=x2 else: max=x3 print("max为:",max)main()
#三者最大值#3.顺序处理,逐步扫描每个值,保留最大者def main(): x1,x2,x3=eval(input("请输入三个值:")) max=x1 if x2>max: max=x2 if x3>max: max=x3 print("max为:",max)main()
#maxn.py#寻找一组数中的最大值def main(): n=eval(input("总共几个数?:")) max=eval(input("输入一个数:")) for i in range(n-1): x=eval(input("输入一个数:")) if x>max: max=x print("最大数为:",max) #print("max为:",max)main()
#maxn.py#终极解决方案def main(): x1,x2,x3=eval(input()) print("最大数为:",max(x1,x2,x3)) #print("max为:",max)main()
6.基本循环结构
for循环语句,循环遍历整个序列,for i in …:
#average1.pydef main(): n=eval(input("sumnumber:")) sum=0.0 for i in range(n): x=eval(input("input:")) sum=sum+x print("\naverage is ",sum/n)main()
for循环缺点:必须提供循环次数
无限循环:while语句可以进行前测循环
continue,break语句
7.通用循环结构
交互式循环
#average2.pydef main(): sum=0.0 count=0 moredata="yes" while moredata[0]=="y": x=eval(input("输入一个数:")) sum=sum+x count=count+1 moredata=input("want another number?(y/n)") print("\nthe average is ",sum/count)main()
#average3.py 哨兵循环def main(): sum=0.0 count=0 #moredata="yes" #while moredata[0]=="y": x=eval(input("输入一个数:")) while x>=0: sum=sum+x count=count+1 #moredata=input("want another number?(y/n)") x=eval(input("输入一个数:")) print("\nthe average is ",sum/count)main()
#average4.py 哨兵循环def main(): sum=0.0 count=0 xStr=input("输入一个数:") while xStr!=" ": x=eval(xStr) sum=sum+x count=count+1 xStr=input("输入一个数:") print("\nthe average is ",sum/count)main()
#文件循环def main(): fileName=input("what file are the numbers in?") infile=openfile(fileName,'r') sum=0.0 count=0 for line in infile: sum=sum+eval(line) count=count+1 print("\nthe average is ",sum/count)main()
#文件循环def main(): fileName=input("what file are the numbers in?") infile=openfile(fileName,'r') sum=0.0 count=0 line=infile.readline() while line!=" ": #for line in infile: sum=sum+eval(line) count=count+1 line=infile.readline() print("\nthe average is ",sum/count)main()
嵌套循环,数字以逗号分隔出现在文件的同一行上。
#嵌套文件循环def main(): fileName=input("what file are the numbers in?") infile=openfile(fileName,'r') sum=0.0 count=0 line=infile.readline() while line!=" ": #为line中的值更新其count和sum for xStr in line.split(","): sum=sum+eval(line) count=count+1 line=infile.readline() print("\nthe average is ",sum/count)main()
8.死循环和循环嵌套
死循环、死语句
while true,while 1 完成特定功能
后测循环,直到型循环,通过while语句间接实现,或break语句实现。
半路循环,出口在中部
9.布尔表达式
true/false
运算符:and,or,not
优先级:not>and>or
阅读全文
0 0
- 【20171001】python_语言设计(2)循环与分支
- 【20171019】python_语言设计(10)库与软件
- 分支与循环(2)
- 【20171002】python_语言设计(3)函数
- 【20171002】python_语言设计(4)文件
- 【20171003】python_语言设计(5)字典
- 【20171011】python_语言设计(6)程序设计方法与面向对象程序设计
- C语言——分支与循环
- C语言 if分支与循环语句
- 分支与循环(3)
- 【20170929】python_语言设计(1)基本语法
- 【20171014】python_语言设计(7)面向过程程序设计
- 【20171014】python_语言设计(8)面向对象程序设计
- 【20171014】python_语言设计(9)交互式图形编程
- unix shell编程(读书笔记2):分支与循环
- 分支判断与循环
- 分支与循环
- Java分支与循环
- HDU 2089 不要62
- C++中两个类中互相引用
- Java基础之随机生成数字和字母
- 从零开始写Python爬虫 --- 1.5 爬虫实践: 获取百度贴吧内容
- 一例当前上下文中不存在名称“model”问题的处理
- 【20171001】python_语言设计(2)循环与分支
- 字符串(string)
- Linux Shell高级技巧(三)
- C#--WinForm保存学员对象的综合验证
- java布局管理器入门详解
- leetcode---combination-sum-ii---dfs
- Hibernate的@Embeddable注解
- css-弹性盒模型
- Coursera 深度学习 deep learning.ai 吴恩达 神经网络和深度学习 第一课 第二周 编程作业 Python Basics with Numpy