FizzBuzz
- Software design approaches and patterns, to identify reusable solutions to commonly occurring problems
- Create logical and maintainable code
- Apply algorithms, logic and data structures
Setup instructions
You’ll find the code in this repo. Follow the instructions laid out in the README to get the project up and running.
Beginning steps
FizzBuzz is a simple exercise that involves counting upwards and replacing certain numbers with words.
Start by writing a program that prints the numbers from 1 to 100.
The problem
Now we’re going to replace certain numbers with words! Try implementing the following rules one at a time:
-
If a number is a multiple of three, print “Fizz” instead of the number.
-
If the number is a multiple of five print “Buzz” instead of the number. For numbers which are multiples of both three and five print “FizzBuzz” instead of the number.
Once you’re happy with your program, show it to one of the trainers for some quick feedback.
Going Further
FizzBuzz is pretty simple as programs go. But it’s interesting to see what happens if you try adding new rules. While working through these next few rules, think about:
- How easy is it?
- How neat and tidy is the resulting code?
- Can you make changes to your program to make these sorts of enhancements easier, or cleaner?
You will obviously need to display more than 100 numbers in order to test out some of these later cases.
Work through the following extra rules, one at a time:
-
If a number is a multiple of 7, print “Bang” instead of the number. For any number which is both a multiple of 7 and a multiple of 3 or 5, append Bang to what you’d have printed anyway. (e.g. 3 * 7 = 21: “FizzBang”).
-
If a number is a multiple of 11, print “Bong” instead of the number. Do not print anything else in these cases. (e.g. 3 * 11 = 33: “Bong”)
-
If a number is a multiple of 13, print “Fezz” instead of the number. If the number is both a multiple of 13 and another number, the “Fezz” goes immediately in front of the first word beginning with B (if there is one), or at the end if there are none. (e.g. 5 * 13 = 65: “FezzBuzz”, 3 * 5 * 13 = 195: “FizzFezzBuzz”). Note that Fezz should be printed even if Bong is also present (e.g. 11 * 13 = 143: “FezzBong”)
-
If a number is a multiple of 17, reverse the order in which any fizzes, buzzes, bangs etc. are printed. (e.g. 3 * 5 * 17 = 255: “BuzzFizz”)
Now that you’ve reached the end, look over your code again. How much of a mess has your code become? How can you make it clear what’s supposed to be happening in the face of so many rules?
Debugging
While we’re here with a simple program, let’s take some time to see some debugging features of your development environment:
- Put a breakpoint on a line early in your code, and run your code by pressing F5 (choose
Python file
option when asked). Note that the program stops when execution hits that breakpoint. While your program is paused, find the ‘Locals’ window using the Variables bar in the top-left of Visual Studio Code. Look at the variables in the Locals dropdown. - Click on the DEBUG menu at the top of the screen. Find out the difference between ‘Step Into’, ‘Step Over’, and ‘Step Out’. You’ll use these a lot, so learn the keys. Until then, maybe write them on a post-it for easy reference. Step through your code and notice how the variables in the Locals window change as you go.
- Hover your mouse over the name of a variable. Notice that a small window pops up showing the current value.
- Find the Call Stack window. This shows which method is currently executing, and all the methods that called it. This will be very useful later.
Stretch Goals
Try these if you finish early, or want to challenge yourself in your spare time.
-
Prompt the user for a maximum number
- Read a value in from the console, then print output up to that number.
-
Allow the user to specify command-line options
- Let the user pass in which rules to implement (e.g. The user might choose 3, 5, and 13 – the “Fizz”, “Buzz”, and “Fezz” rules, but no “Bang” or “Bong”) as a command line parameter (or via some other means of your choice).
- If you wanted to go wild and let the user define their own rules, how would you do that?