Discuss / Python / 作业

作业

Topic source

-Mr_Lu_____

#1 Created at ... [Delete] [Delete and Lock User]
import math


def quadratic(a, b, c):
    for s in (a, b, c):
        if not isinstance(s, (int, float)):
            raise TypeError('输入错误')
    if a != 0:
        delta = b * b - 4 * a * c
        if delta >= 0:
            return (-b + math.sqrt(delta)) / (2 * a), (-b - math.sqrt(delta)) / (2 * a)
        else:
            return '此一元二次方程无实数根'
    else:
        return '错误,a不能等于0'


print(quadratic(2, 3, 1))
print(quadratic(1, 3, -4))
print(quadratic(0, 3, 1))
print(quadratic(1, 0, 1))

-Mr_Lu_____

#2 Created at ... [Delete] [Delete and Lock User]

很多人都忽略了题目,是返回”一元二次方程的解“。

一元二次方程二次项系数a不能为0,如果为0那就是一元一次方程了。


  • 1

Reply