python - PySchools topic 3 Q8 -


the question is

for quadratic equation in form of ax2 + bx + c, discriminant, d b2-4ac. write function return following output depending on discriminant.

  • d > 0: 2 real roots.
  • d = 0: 1 real root.
  • d < 0: 2 complex roots.

examples

>>> quadratic(1, 2, 3)     'this equation has 2 complex roots.' >>> quadratic(1, 3, 2)     'this equation has 2 real roots.' >>> quadratic(1, 4, 4)     'this equation has 1 real root.' 

python gave "private test cases failed" error. error?

def quadrtic(a,b,c): d=b**2-4*a*c if d<0:     return "this equation has 2 complex roots." elif d==1:     return "this equation has 2 real roots." elif d==0 or d==1:     return "this equation has 1 real root." 

your if blocks should be

def quadrtic(a,b,c):    d = b**2 - 4*a*c    if d < 0:        return "this equation has 2 complex roots."    elif d > 0:        return "this equation has 2 real roots."    else:  # d == 0        return "this equation has 1 real root." 

the discriminant unlikely == 1. have 2 real roots, must greater 0, discriminant can real number in case (e.g. 4.2564)


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -