解一元二次方程Python
时间: 2023-05-20 20:05:28 浏览: 99
python编程求解一元二次方程问题分析.doc
5星 · 资源好评率100%
Sure, solving a quadratic equation in Python can be done using the quadratic formula. Here's an example code:
import math
a = float(input("Enter coefficient of x^2: "))
b = float(input("Enter coefficient of x: "))
c = float(input("Enter constant term: "))
# calculate discriminant
discriminant = b**2 - 4*a*c
if discriminant < 0:
print("No real roots")
elif discriminant == 0:
root = -b / (2*a)
print("One real root: ", root)
else:
root1 = (-b + math.sqrt(discriminant)) / (2*a)
root2 = (-b - math.sqrt(discriminant)) / (2*a)
print("Two real roots: ", root1, " and ", root2)
Hope this helps!
阅读全文