Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
Loading…
Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
Loading…
Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
PYTHON FUNCTIONS / CHAPTER 04 / Learn / LESSON 05
Fifth class. Before you compute, check the inputs. If they are invalid — return early. Still no try/except.
def safe_bmi(weight, height):
if weight <= 0 or height <= 0:
return None
return weight / height ** 2None here means “no valid result”. The caller can check and react.
Common mistake: dividing without a check and hitting ZeroDivisionError; or continuing with negative values “because the formula still runs”.
Learn: check with if and early return — still no try.
AWAITING TYPES
›