Good place to stop if you're short on time — step 3 picks up here.
Lesson idea Tap to fold
What tells Python which lines are inside a loop?
Show the answer
Answer. The indent — the four spaces at the start of the line. Python uses that spacing instead of brackets.
Return a reusable result
Your outcome: Define and call a function whose parameters control the result, including new names and zero frames.
def names a function and describes its parameters. Defining it does not execute its body. Calling frames_to_seconds(240) supplies 240 to the parameter frames for that call.
return hands a value to the caller and ends that call. print only displays a value. A function that only prints and reaches its end returns None. Keep display outside the helper so the caller can calculate with the returned number.
The / operator divides and produces a floating-point result: 240 / 24 is 10.0. This exercise declares a fixed rate; the 25-fps variant changes that denominator once inside the helper. It does not analyze a clip or discover its actual rate.
A function can receive two parameters, separated by a comma. To build slate text with +, convert the numeric frame count with str(frame_count) first. str makes display text without changing the caller’s integer. Either quote style is fine; a correct function need not match the example source.
# Return a reusable result
def frames_to_seconds(frames):
return frames / 24
seconds = frames_to_seconds(240)
print(seconds)
print(frames_to_seconds(96))
def slate_line(name, frames):
return name + " · " + str(frames) + " frames"
print(slate_line("Canal", 48))
Trace: The first definition creates a callable. The first call returns 10.0 to seconds, then print displays it. The second call returns 4.0. The two-parameter slate call returns Canal · 48 frames.
Explain the call, parameter and returned value separately. Change both call inputs; do not call a copied output proof of a reusable function.
Predict, explain, then test
Answer before opening the comparison. These brief answers are self-reviewed, not quiz scores. Task labels describe the brief, not proof that you worked without assistance. Record any examples, hints or tutor help in your own Notes.
Predict
What does frames_to_seconds(0) return at 24 fps?
Compare your answer · self-reviewed
0.0, a numeric zero. No frames is not a NameError or an absent return.
Explain
Why does a helper that only prints a slate fail when the caller needs its returned text?
Compare your answer · self-reviewed
It returns None, not the printed string. The caller’s print could then show an extra None. Return the text and print it at the outer boundary.
Repair
The parameter is frames, but the return uses an undefined count. What is the reusable repair?
Compare your answer · self-reviewed
Use the supplied parameter in the calculation. Replacing count with a fixed sample such as 96 only works for that sample.
Choose an exercise to load its prompt.
Today's idea in two other voices, if you want one. Not required — the exercises above are the actual lesson.
- Defining Your Own Python Function — article, Real Python
- Python Tutorial for Beginners 8: Functions — video, Corey Schafer
Loading exercises…
Noted on your route map, with the step you were on. Nothing is lost by parking it — the next review day will bring this idea back, and the flag tells the course where to slow down.
The tutor mounts here when JavaScript is available. The lesson above stays readable without it.