程序基本结构和简单分支
来源:互联网 发布:道亨软件多少钱 编辑:程序博客网 时间:2024/05/20 10:14
任何算法(程序)都可以用顺序、选择、循环这三种结构来实现程序框图。
例如:输入PM2.5来判断空气质量
如果值大于75,输出空气污染警告,如果小于75,输出空气良好。
# pm25.py# 空气质量提醒def main(): PM=eval(input("What is today's PM2.5?")) #打印相应提醒 if PM>75: print("Unhealthy. Be careful!") else: 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()
输入输出
This program finds the real solutions to a quadraticplease enter the coefficients(a,b,c)(1,2,1)The solutions are : -1.0 -1.0>>>
上面程序是不完美的,因为没有实根的情况下是错误的,所以改进后需要判断语句来判断delta=b*b-4*a*c
是否大于等于0或者小于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 delta<0: print("\nThe quation has no real roots!") else: discRoot=math.sqrt(delta) root1=(-b+discRoot)/(2*a) root2=(-b-discRoot)/(2*a) print("\nThe solutions are :",root1,root2)main()
阅读全文
0 0
- 程序基本结构和简单分支
- 分支结构程序
- Python-程序分支结构
- JAVA-基本数据类型-分支结构
- 多分支选择结构程序
- c语言程序 分支结构
- 第三章 分支结构程序
- C程序的基本结构与简单规范
- ios学习第一天(四)oc基本结构顺序分支和循环
- C语言分支结构和循环结构
- python的分支结构和循环结构
- 二、分支结构和循环结构
- (谭版)5 分支结构程序
- 第五章 分支结构程序 (答案)
- 第七讲:分支结构程序体验
- 分支结构练习-5.数字Game程序
- 7 分支结构程序体验 项目
- Erlang 分支结构程序与保护式
- bfs 大逃亡
- 「LibreOJ β Round #4」求和 莫比乌斯函数
- 调用摄像头功能的方法详解
- 35、异步编程和多线程编程的区别
- UVA 1189
- 程序基本结构和简单分支
- 【洛谷】P1455 搭配购买
- iOS 使用UICollectionView实现轮播图
- 函数节流与防抖的实现
- UVA 12101
- selenium webdriver 学习总结-浏览器启动方式(二)
- Redis实现访问流量控制
- Paper read weekly(Four)
- 回溯法求解全排列问题