h.Habits
← All guides

Learning · from following to trying

After a Python tutorial, build one small program without copying.

Turn a short requirement into code, check what it returns, and use one failed example to decide what to change.

Close the worked solution and write down what your program should do. Make a first attempt, run a few examples, then compare the results with the requirement. You can still look up syntax in the Python documentation.

This exercise is for someone who has met lists, for, if and def in a beginner lesson. Use the Python 3 editor or interpreter from your course. If those ideas are new, work through a guided example first, then return here.

Start with a job small enough to check.

Original practice exercise

Count the reading sessions that reached 20 minutes.

Write a function called count_sessions(minutes). Its input is a list of nonnegative whole numbers, each representing one session’s minutes. Return the number of sessions lasting at least 20 minutes.

  • A session lasting exactly 20 minutes counts.
  • An empty list returns 0.
  • Count sessions; do not add their minutes together.

For this exercise, assume the input follows that contract. Handling text, negative numbers or other invalid input can be a separate task.

Before writing code, work out these answers yourself. Keep them beside your editor so the examples tell you whether your attempt meets the requirement.

Expected results
Input minutesSessions that count
[12, 20, 35]2
[]0
[19, 20, 21]2
[40]1

Run the function, not just the file.

Make your own attempt before reading the worked version below. Put these lines after your function definition in the same Python file, then use your editor’s Run command. The loop calls your function once for each input and prints its answer.

samples = [
    [12, 20, 35],
    [],
    [19, 20, 21],
    [40],
]
for sample in samples:
    print(count_sessions(sample))

The correct output is four lines, in the same order as the table:

2
0
2
1

If nothing appears, check that you included the calls after the function. Defining a function alone does not run it. If Python reports an error, read the message and line number before changing the logic.

A boundary mistake gives you a useful next move.

Here is an intentionally incorrect attempt. Run it with the same test calls:

def count_sessions(minutes):
    completed = 0
    for duration in minutes:
        if duration > 20:
            completed += 1
    return completed

It prints 1, 0, 1, 1, each on its own line. The first and third answers are wrong. Both examples contain a session lasting exactly 20 minutes.

The condition duration > 20 excludes that session. The requirement says “at least,” so change it to duration >= 20:

def count_sessions(minutes):
    completed = 0
    for duration in minutes:
        if duration >= 20:
            completed += 1
    return completed

Run the same four calls again. This version returns 2, 0, 2, 1. Keep the empty-list and single-session examples: they check different situations from the boundary case.

Explain the change in one sentence in your notes: “I used greater than, but exactly 20 should count too.” That gives your next session something concrete to return to.

Change one requirement on your next attempt.

Now let the caller choose the target. Write count_sessions(minutes, target), where target is a positive whole number. Count sessions lasting at least that many minutes. Try your own version before checking the worked answer.

For [12, 20, 35], a target of 15 should return 2; a target of 30 should return 1.

One version to compare with yours

def count_sessions(minutes, target):
    completed = 0
    for duration in minutes:
        if duration >= target:
            completed += 1
    return completed

Replace the earlier function with this two-argument version, then replace the earlier test calls with these:

sample = [12, 20, 35]
print(count_sessions(sample, 15))
print(count_sessions(sample, 30))

Expected output:

2
1

If your next hurdle is remembering the loop syntax, revisit that part of your lesson, close it, and recreate the loop. If the syntax is familiar but the result is wrong, write down a smaller input and trace what happens to completed after each item.

End the session with one saved attempt, the examples you ran, and a next change. These small checks show what this function does; broader programming skills need further problems.

Make your next coding session easy to return to.

Add a habit such as “Try one Python change and run its examples” in Habits, then check it off after the session. Keep your code and test output in your editor.

Download for iPhone ↗Try the web app →

Source

The official Python tutorial on control flow explains conditions, loops and function definitions. The reading-session exercise and worked code are original examples from Habits.