Coding Curriculum - Python

Letters

We will use a lot of the concepts that we have already learned like loops, importing and so on. However we will learn one more thing. That is how to populate the grid with random letters. But before I give you a solution, think about a solution for about 5 minutes.

Whenever I ask this question, most people think of two solutions. Both solutions involve creating an array with all of the letters in the alphabeth. Then the first one uses randint(0, len(array) - 1) to find a random element. This would work, but it is not an elegant solution. The second one is a bit better and simply uses choice(array). Whilst both solutions are definitely not wrong, I believe they are not the best ones. This is because we, programmers, are very lazy. Therefore we do not have time to create an array with all of the letters in the alphabeth! Instead we use, you guessed it, an import statement.

The string library contains a variable called letters, which is a string with all of the letters in the alphabeth. Hence, we can use:

from string import letters