Recall the has_roots function that we wrote.
def has_roots(a, b, c):
'''Return True iff ax^2+bx+c=0 has real roots
[Note: Return True iff ... means, in Python (but not in math!) Return
True if ax^2+bx+c=0 has real roots and False otherwise]
Arguments:
a, b, c -- floats
'''
disc = b**2-4*a*c
This is not the nicest way to write this -- we'll rewrite this function later
if disc >= 0:
return True
else:
return False
Let's rewrite it a bit:
def has_roots(a, b, c):
disc = b**2-4*a*c
return disc >= 0
This does exactly the same thing: since the value of disc >= 0 is True exactly when $disc\geq 0$, we return True when $disc\geq 0$, and False when $disc < 0$.
Let's not write a similar function, has_no_roots. Here's is one option:
def has_no_roots(a, b, c):
disc = b**2-4*a*c
return disc < 0
Here's another:
def has_no_roots(a, b, c):
disc = b**2-4*a*c
return not (disc >= 0)
Finally, here's the laziest option:
def has_no_roots(a, b, c):
return not has_roots(a, b, c)
All these functions compute the same thing.