Skip to content

Loop Drills

You'll write six small programs using nothing but variables, conditionals and loops, which is the muscle under everything else you'll ever code.

About

Nobody stays stuck on syntax for long. Where people get stuck is thinking in steps, and these drills are the push-ups for that. Each one is small enough to finish in an hour and annoying enough to teach you something. The spaces on the left of the pyramid will be wrong three times before they're right, and that's normal. No libraries and no copying solutions here. If you get stuck, write the steps out in plain English first and then turn each line into code.

At a glance

Per requirement
15 XP
Completion bonus
75 XP
Requirements
6
Takes
One weekend
Skill level
Novice (never tried it)
Cost
Free
Supplies
Nothing special
Where
At home
Other people
On your own

Requirements

  1. 1
    Write FizzBuzz for 1 to 100, then change the rules and do it again. Multiples of 3 print Fizz, multiples of 5 print Buzz, and multiples of both print FizzBuzz. Then swap in 7 and 11 with new words of your own. The second version is the real test, since most people have the first one memorized by now. Take a screenshot of both outputs. Photo
  2. 2
    Print the first 30 Fibonacci numbers without using recursion. Two variables and a loop will do it. Check the 30th number against a reference, which is 832040. If yours disagrees, you're off by one somewhere, and off-by-one is the most common bug you'll ever write. Take a screenshot of the output. Photo
  3. 3
    Tell whether a number is prime, then list every prime under 1000. Try dividing by everything up to the square root of the number. There are 168 primes under 1000, so count yours. Take a screenshot showing the count and the last few primes on the list. Photo
  4. 4
    Reverse a string and check whether it's a palindrome, ignoring case and spaces. Do it with a loop first rather than the built-in reverse, so you see how the characters move. Test it with the phrase 'Never odd or even'. Take a screenshot of three different inputs and their answers. Photo
  5. 5
    Print a multiplication table as a neatly aligned grid. Use nested loops from 1 to 12. The columns have to line up, which means padding the numbers with spaces, and that padding step is where most people learn how string formatting works. Take a screenshot of the grid. Photo
  6. 6
    Draw a pyramid of stars at any height the user types in. Height 5 means a centered triangle five rows tall. The spaces on the left are the tricky part, so work them out on paper first. Then flip it upside down. Take a screenshot of both versions at height 7. Photo