用Python实现解一元二次方程,来自廖雪峰Pyhton3.0教程课后习题
# -*- coding: utf-8 -*-import mathdef quadratic(a, b, c): if (b * b - 4 * a * c) < 0: return 'None' Delte = math.sqrt(b * b - 4 * a * c) if Delte > 0: x = (- b + Delte) / (2 * a) y = (- b - Delte) / (2 * a) return x, y else: x = (- b) / (2 * a) return x# 测试:PRint(quadratic(2, 3, 1)) # => (-0.5, -1.0)print(quadratic(1, 3, -4)) # => (1.0, -4.0)新闻热点
疑难解答