Skip to content
CodeItRaw
Basics

Lesson 7/7

for knows from the start how many turns it takes. Sometimes you cannot know: "until the number runs out", "until the wall". while asks a question and keeps going as long as the answer is true.

steps = 0
while key > 1:
key = key // 2
steps += 1
return steps
// is whole division: it drops the fraction (7 // 2 is 3). This loop halves the number each turn and counts how many turns it takes to reach 1.

Ask yourself two things about every while: is there something inside the loop that changes the condition, and is my answer right if the loop never runs? The second usually shows up at the edges, with 0 or an empty list.

Tasks

Tasks open in order. Solve them all and the next lesson opens.

This lesson's tasks open when the lessons before it are finished. You can read the explanation now.Go to the current lesson

  1. 01

    How Many Digits?

    Function

  2. 02

    Count Your Way Back

    Infiltration

If you would rather not wait for the order, every problem is open without locks: Problem list