Discuss / Python / 交作业了

交作业了

Topic source

少年瘾

#1 Created at ... [Delete] [Delete and Lock User]
import math   #调用math函数def quadratic_equation(a,b,c):    #定义函数    discriminant = b * b - 4 * a * c    #计算判别式    if discriminant > 0:  #判定是否大于1,若大于有两个值        root1 = (-b + math.sqrt(discriminant)) / (2 * a)      #套入公式计算         root2 = (-b - math.sqrt(discriminant)) / (2 * a)        return root1, root2    #返回结果    elif discriminant == 0:   #判定判别式为0时        root1 = -b / (2 * a)    #套入计算公式        return root1  #返回结果    else:    #若判别式小于0时:        return "无实数根"a = int(input("请输入二次项系数a:\n"))b = int(input("请输入一次项系数b:\n"))c = int(input("请输入常数c:\n"))roots = quadratic_equation(a, b, c)   #定义变量rootsprint("方程式的根为:", roots)   #输入结果

  • 1

Reply