Can I learn python in one month? Day 27

Adam Jones
3 min readMar 28, 2018

--

Over the past week I’ve successfully managed to set up a coding environment for python. As a complete noob this is something that I was a little unsure about — what is the optimal setup? The ‘beginner python’ sites I found don’t talk you through how to actually set up an environment to write and execute python scripts step by step (presumably too basic for them to cover). So specifically what I did was to download Python 2.7.3 for Mac and use Sublime Text 2 to write the code. I was then running the script through the Terminal by navigating to the appropriate folder and executing the file before realizing that you could just execute directly in Sublime Text. Turns out it’s pretty simple really.

Setup complete, this weekend I decided to apply my newly learned python skills to one of the challenges I set for myself: to create a random country generator. I knew the python for this would be fairly straight-forward, but the challenge was pulling in data from a source file. I found a list of all 196 countries and their capital cities on a website and saved them as a csv file in the same folder as my scripts. After some searching on stackoverflow I found the module to import (I used the csv module), figured out that each row was being imported as a separate list (containing country + capital city) and then wrote a script to create a list of countries. I then used the random module to select a country from the list at random. Challenge complete!

Admittedly this is quite basic. So next I decided to make a game for my wife where you have to guess the capital city for a given country (needless to say, she was delighted with her new game). The script selects a country at random and asks you to guess the capital city. Once answered correctly, that country is removed from the pool of countries to select from until all countries are answered correctly (or you get bored). So here’s the script:

And it plays something like this:

As you can see I was able to ignore the specific capitalization of words but the spelling and punctuation has to be an exact match which is quite annoying when playing. Still, it works and is actually quite fun.

There’s still a few days left of the month so I’ll see where I can get to on one of my other python challenges.

Read the previous post here and the next post here

--

--