Python一日一练03----一元二次方程的计算
2015-01-18 22:43
169 查看
要求
由用户随意给定一元二次方程系数a、b、c,计算一元二次方程的解
源码
import cmath #包含复数运算 import math import sys def get_float(msg, allow_zero): x = None while x is None: try: x = float(input(msg)) if not allow_zero and abs(x) < sys.float_info.epsilon: #sys.float_info.epsilon是浮点数0 print("zero is not allowed") x = None except ValueError as err: print(err) return x print("ax\N{SUPERSCRIPT TWO} + bx + c = 0") #\N{name}给指定的Unicode字符 a = get_float("enter a: ", False) b = get_float("enter b: ", True) c = get_float("enter c: ", True) x1 = None x2 = None discriminant = (b ** 2) - (4 * a * c) if discriminant == 0: x1 = -(b / (2 * a)) else: if discriminant > 0: root = math.sqrt(discriminant) else: # discriminant < 0 root = cmath.sqrt(discriminant) x1 = (-b + root) / (2 * a) x2 = (-b - root) / (2 * a) equation = ("{0}x\N{SUPERSCRIPT TWO} + {1}x + {2} = 0" " \N{RIGHTWARDS ARROW} x = {3}").format(a, b, c, x1) if x2 is not None: equation += " or x = {0}".format(x2) print(equation)
欢迎收听我的微信公众号
相关文章推荐
- Python一日一练03----输出网页
- 【量化小讲堂-Python&Pandas系列03】使用python计算移动平均线
- 用python计算top命令中某个进程的CPU,Memory使用情况
- python 中如何计算时间差...
- Python入门的36个例子——03 Python 中的数“只有”四种类型
- 03-网页内容的编码检测 | 02.数据解析 | Python
- 03-PubSubHubbub 和 twisted 的 Persistent connections 能力 | 07.杂项 | Python
- Python 计算不重复元素的个数
- 提前还贷的python计算程序
- 学习Python列表内涵:一行代码搞定双倍超立方数计算
- python-05-03 python3.0 的语法变动
- asp.net 中计算某月总共有多少天 最后一日是几号。
- 一句代码计算阶乘(python)
- python天天进步(4)--计算时间差
- python 计算时间差
- 一句代码计算阶乘(python)
- 用python 在c++计算数学表达式的例子
- 查表法计算crc的python演示程序
- [转]使用python调用计算所分词
- python 计算下一个星期三的日期