It's not that I'm so smart, it's just that I stay with problems longer. - A. Einstein
Hi Pythonistas, it’s time to do things differently! Test Driven Development has to be one of the hardest programming methodologies to adopt yet also one of the most satisfying. It forces a different mindset which is definitely not for everyone. Either way, put your thinking caps on and jump in!
An excerpt from this cool TDD tutorial on Tuts+ sums it up nicely:
TDD, in its most basic terms, is the process of implementing code by writing your tests first, seeing them fail, then writing the code to make the tests pass. You can then build upon this developed code by appropriately altering your test to expect the outcome of additional functionality, then writing the code to make it pass again.
The challenge is to use TDD of course but to make what? Rather than leave this open ended, we’re going to ask you to use TDD while solving a kata featured in PyConEs 2016 Introduction to TDD Workshop (translation).
FizzBuzz is a simple problem wherein your program will print the numbers from 1 to 100. The tricky part is that if a number being printed is divisible by 3, the program returns “Fizz” instead of the number.
Likewise, print “Buzz” for any number divisible by 5 and “FizzBuzz” for any number divisible by 3 and 5.
It’s a very simple problem to solve which is why we choose it to focus on learning / practicing TDD!
Code a solution to the FizzBuzz problem using TDD and the challenge is complete. Make sure to PR your solution and your test file.
We’re aware that you can simply submit a PR with a test file you’ve created after writing your script but clearly that defeats the purpose of this challenge!
This challenge is intended to get you to try using TDD. There are no winners or losers so there really would be no point in writing the tests after! For that, feel free to try Code Challenge 39!
You like these challenges? We have published quite a few and we're not planning to stop anytime soon.
You really like them and plan on PR'ing more in the future? Then consider joining our private Slack channel sending us an email. This way you get the unique opportunity to learn from other passionate Pythonistas and share some of your experience.
You can PR your work to our Community branch of our Challenges repo. We will include it in our review post. Our PR template also lets you reflect a bit on your learning and provide some feedback how we can keep our challenges interesting. See detailed instructions here.
Our goal is to learn and teach you Python through practical exercises. Learning a programming language is way more fun as a community!
Keep Calm and Code in Python!
Do you want to get 250+ concise and applicable Python tips in an ebook that will cost you less than 10 bucks (future updates included), check it out here.
"The discussions are succinct yet thorough enough to give you a solid grasp of the particular problem. I just wish I would have had this book when I started learning Python." - Daniel H
"Bob and Julian are the masters at aggregating these small snippets of code that can really make certain aspects of coding easier." - Jesse B
"This is now my favourite first Python go-to reference." - Anthony L
"Do you ever go on one of those cooking websites for a recipe and have to scroll for what feels like an eternity to get to the ingredients and the 4 steps the recipe actually takes? This is the opposite of that." - Sergio S