回覆列表
  • 1 # 使用者3136867717565

    2,4,2算出來小於0了,程式走了你的else,然後else這裡又沒有給x1,x2賦值,怎麼返回?

    def quadratic(a,b,c):

    d=b**2-4*a*c

    if d>0:

    x1=(-b+d**0.5/2/a)

    x2=(-b-d**0.5)/2/a

    print("The quadratic is :{0}x^2+{1}x+{2}=0".format(a,b,c))

    return x1,x2

    else:

    print "There is no real root"

    return None,None

    x1,x2=quadratic(2,4,2)

    #There is no real root

    print x1,x2

    #None None

    x1,x2=quadratic(2,16,2)

    #The quadratic is :2x^2+16x+2=0

    print x1,x2

    #-12.1270166538 -7.87298334621

    https://iknow-pic.cdn.bcebos.com/09fa513d269759ee2b164638b8fb43166c22dffa

  • 中秋節和大豐收的關聯?
  • 屬羊的水瓶男和屬豬的雙子女配麼?