Boss lesson · Unit 02
FizzBuzz and friends
Loops and conditions working together
What you practise
- Loops and conditions together: FizzBuzz, summing digits, testing for a prime, the off-by-one error found and fixed
Try a question from this lesson
Pick one
FizzBuzz: count up from 1. Say Fizz for a multiple of 3, Buzz for a multiple of 5, FizzBuzz for a multiple of both, else the number. What is said for 15?
Correct answer
FizzBuzz
Why
15 is 3 * 5, so it is a multiple of both and gets the combined word. This little game is a famous first exercise because it needs a loop, a chain of conditions and care about their order.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Narrowest test first
In an if/elif chain only the first true block runs.
So when one question is a special case of another, ask the special one first. "Multiple of 15" is a special case of "multiple of 3": every multiple of 15 passes both tests.
A loop around the chain then plays the game for every number in turn.
Concept
Testing for a prime
A prime is a whole number above 1 that nothing divides exactly except 1 and itself. 7 is prime; 9 is not, because 3 divides it.
To test a number n, try every possible divisor from 2 up to n - 1. Start by assuming n is prime. One exact division is enough to prove that it is not.
10 more exercises in the app
- 3× multiple choice
- 3× type the answer
- 2× fill the blanks
- 1× order the lines
- 1× true or false
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.