
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 namedis_heavy.kgis the recipe's input. It holds whatever value you put in the brackets, like80or10.- The indented lines are the recipe's steps.
returnhands the answer back. Thenprint(...)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):
- If
oilis greater than0, return"lit". - Otherwise, return
"dark".
Complete lantern_status(oil):
- If
oilis greater than0, return"lit". - Otherwise, return
"dark".
main.py Python sleeping
Press Run to try your code, then Check to test it.