Lesson 6 of 10

The for loop

Goal

Visit every item

01

Explanation

A for loop walks through a list's items one by one: you write for s in scores: with no counter and no index numbers. When you need a run of consecutive numbers, use range(1, 11), which gives you 1 through 10, because the start is included and the end is not. That's behind most off-by-one errors, where the count comes out one too high or one too low.

If you need the position along with the value, don't write range(len(scores)). Use enumerate(scores, start=1), which gives you both at once: for pos, s in enumerate(scores, start=1):.

continue skips the rest of the current iteration and moves on to the next item, and break ends the whole loop. You'll need both in this lesson, because here None means the student was absent, and comparing it to a number, as in None >= 50, raises a TypeError and stops the program, so skip it before any comparison.

02

Your task

Use range to build the list multiples with the multiples of 7 from 7 to 70. In passed, count how many scores are 50 or higher, skipping absent students. Then set first_fail to the position of the first score below 50, counting from 1, and stop the loop there.

Checks

4 checks
  1. multiples has ten numbers
  2. multiples runs from 7 to 70
  3. passed counts the passing scores and skips absent students
  4. first_fail is the position of the first failing score, counting from 1
03

Hints

0/3
Track lessons0 of 10 done

Start with the most beginner-friendly language there is, and use it to learn variables, strings, lists, conditions, loops and functions.

Beginner level, 10 lessons, 2 hours in total.

main.py
OutputPython (Pyodide)

Loading Python. This only happens once. Your browser keeps it after that.

–/4

Checks start once the environment is ready.

4 checks listed under “Your task”.

Track lessons0 of 10 done

Start with the most beginner-friendly language there is, and use it to learn variables, strings, lists, conditions, loops and functions.

Beginner level, 10 lessons, 2 hours in total.