一元二次方程求解python
时间: 2023-09-25 19:17:22 浏览: 142
要在Python中求解一元二次方程,可以使用以下代码实现:
```python
import math
def solve_quadratic_equation(a, b, c):
if a == 0:
return "不是二次方程"
if b == 0:
x = 0
if -c/a >= 0:
return x, math.sqrt(-c/a)
else:
return "无实根"
if c == 0:
x1 = 0
x2 = -b/a
return x1, x2
delta = b**2 - 4*a*c
if delta < 0:
return "无实根"
elif delta == 0:
x = -b / (2*a)
return x
else:
x1 = (-b + math.sqrt(delta)) / (2*a)
x2 = (-b - math.sqrt(delta)) / (2*a)
return x1, x2
```
这段代码首先引入了math包,然后定义了一个solve_quadratic_equation函数来求解一元二次方程。函数中先判断了输入的系数a、b、c是否符合二次方程的要求。然后根据判定的结果进行相应的计算,最后返回方程的解或者无解的提示。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [接一元二次方程的几种解法,用python代码实现](https://blog.csdn.net/xifenglie123321/article/details/131530182)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* [Python实现求解一元二次方程的方法示例](https://download.csdn.net/download/weixin_38655780/14912139)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文