Additionally, there’s also the case of a == b
to consider—right now, your manual commands (or the function using print()
directly) doesn’t print anything, and the function above returns None
(which is implicit if the function exits without a return statement, though it is a good practice to make that explicit if the function uses return
elsewhere, i.e.
def both(a, b):
if a > b:
return True
if b > a:
return False
return None
However, there’s a good chance that you want to treat a == b
as either good, or bad. In that case, you can simplify your function dramatically; if, say, a == b
is good
, then you could write it:
def both(a, b):
return a >= b
Or if a == b
is bad
, you’d write it
def both(a, b):
return a > b
This returns the boolean result of the comparison directly, with no need for separate branches.
Alternatively, it may be closer to what you are asking for (to be able to do print(math)
if you have your function instead return the string "good"
or "bad"
and print that. For that, you can do (assuming a == b
is "bad"
)
def good_bad(a, b):
if a > b:
return "good"
else:
return "bad"
Or, more concisely:
def good_bad(a, b):
return "good" if a > b else "bad"
You can then use it like so:
print(good_bad(2, 3))