Lesson 3/6High school~90 min
The while loop
A loop that runs while a condition holds: counting down, skipping numbers, reading repeatedly until an end value, and handling bad input.
You can solve exercises without signing in; sign in and your progress is kept.
After this lesson you'll be able to:
- Write a
whileloop with and without a counter - End a loop with
breakand skip a step withcontinue - Avoid an infinite loop
What you practise here
- while loops
- Reading numbers from input
- The if statement
- Integer division and remainder: // and %
- Input with input()
- Text: the str type
- Formatting output: sep, end, f-strings
- Comparisons: ==, !=, <, >=
- Logical operators: and, or, not
- Two branches: if – else
+ 2
Real programming tasks: write a program, run it and submit it to be checked, hidden tests included.
Start the challenges- Write a programTime in secondsElementary20 XP
- Write a programGuess the wordBasic10 XP
- Write a programNumbers without 3, 7 and 14Basic10 XP
- Write a programEven numbers up to a limitBasic10 XP
- Write a programCharacters and their positionsBasic10 XP
- Write a programCounting down to 5Basic10 XP
- Write a programOdd numbers except 17Basic10 XP
- Algorithmic problemThe Collatz sequenceElementaryOptional20 XP
- Algorithmic problemTo binaryElementaryOptional20 XP