这篇教程Python 二次方程写得很实用,希望能帮到您。 以下实例为通过用户输入数字,并计算二次方程: 实例(Python 3.0+) import cmath a = float(input('输入 a: '))b = float(input('输入 b: '))c = float(input('输入 c: ')) d = (b**2) - (4*a*c) sol1 = (-b-cmath.sqrt(d))/(2*a)sol2 = (-b+cmath.sqrt(d))/(2*a) print('结果为 {0} 和 {1}'.format(sol1,sol2)) 执行以上代码输出结果为: $ python test.py 输入 a: 1输入 b: 5输入 c: 6结果为 (-3+0j) 和 (-2+0j) 该实例中,我们使用了 cmath (complex math) 模块的 sqrt() 方法 来计算平方根。 Python 平方根 Python 计算三角形的面积 |