Code

Pip's Recipe Box

Pip wants to test a decision with lots of different values. Typing new input every time is slow! So Pip packs the decision into a function: a named recipe you can use again and again.

def is_heavy(kg):
    if kg > 50:
        return "heavy"
    else:
        return "light"

print(is_heavy(80))   # heavy
print(is_heavy(10))   # light

Here's what each part means:

  • def is_heavy(kg): creates a recipe named is_heavy.
  • kg is the recipe's input. It holds whatever value you put in the brackets, like 80 or 10.
  • The indented lines are the recipe's steps.
  • return hands the answer back. Then print(...) can show it.

You'll learn to build your own functions in the Functions course. For now, Pip writes the def line for you, and you fill in the inside. The next few lessons work this way.

Assignment

Complete lantern_status(oil):

  1. If oil is greater than 0, return "lit".
  2. Otherwise, return "dark".