Discuss / Python / 一元二次方程

一元二次方程

Topic source

FW小菜鸡

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

import math

def quadratic(a, b, c):

    num = b * b - 4 * a * c

    if num < 0:

        return '该方程式无解:%sx^2 + %sx + %s = 0' % (a, b, c)

    elif num == 0:

        x = -(b / 2 * a)

        return x

    else:

        x1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 * a

        x2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 * a

        return x1, x2

list = []

liststr = ['a', 'b', 'c']

for num in liststr:

    list.append(int(input('请输入%s:' % (num))))

print(quadratic(list[0],list[1],list[2]))


  • 1

Reply