The way I learnt to program was by programming. I found reading books and the internet are great to solve specific problems, and for guidance. However the only way to learn to program is to sit down and actually program!
Once I had progressed from the 'Hello World!' stage, one of the first programs I wrote was a word checker. The idea behind the program is the software will ask the user to enter a word. The program will then check if this is a real word or not, by checking it against a dictionary. It will then report back whether there was a match in the dictionary or not.
It's a very simple program but it did introduce me to a few new concepts such as loading files and writing functions.
I thought I would not only share this program with you, but explain my thought process so you could have a look at how I approached it and end up with a program that you will hopefully find useful.
This program is written using Python 2.7. There are some differences between Python 2.7 and Python 3.0, and although these easy to overcome you will need to use Python 2.7 for this tutorial. The reason I use Python 2.7 is because I required certain libraries for some more advanced programming, which were not available for Python 3.0.
It is always worth breaking your program down into smaller chunks. Thinking about a program as a whole can be quite daunting. However when you break it down smaller into smaller sections, each section seems quite easy to do.
So how would I plan my word checker program? Mapping out my program I figured there are 4 stages to it.
- Stage 1 - I need to be able to type in a word to check if its a real word.
- Stage 2 - I then need to be able to open up a dictionary of real words to check against.
- Stage 3 - I then need to compare my word with the words in the dictionary.
- Stage 4 - If my word matches with a word in the dictionary I need to report this is successful. If it fails to match then I also need to report this back.
Ok now we know what we have to do lets get on with it!
To start programming in Python click on the IDLE icon on the desktop, ensure you don't click on the IDLE 3 Icon!
This should open the following window.
Click on File and then New Window and an empty text box should open. This is what you will type your program into.
Stage 1 - Inputting a test word.
Right first things first, we need to be able to input a word. We also know that we are likely to use that word again, so lets ensure we store that word somehow so we can access it again in the future. Lets create a variable in which we will store our word, lets call this variable inputtedWord.
So we want to make the word equal to some input from the keyboard. Python allows us to do this easily with the raw_input command.
inputtedWord = raw_input ('Please input a word:').lower()
As you can see the command also allows us to enter some text to explain to the user what you are asking them to enter, in this case we have asked to 'Please input a word:',
Finally we finish with .lower() As computers differentiate between upper and lower text, lets make our life simple and work in lower case text only. This command converts whatever is typed into lowercase.
Before moving on lets test this to see if it works.
Under the line you have just inputted type
Now press F5 to run your program. Python will ask you to save your file at this stage, so call it WordChecker.py.
Click on OK.
Choose where you want to save your program, if you are unsure where to save it, click on Desktop.
In Filename type:
Then click on Save and the program should run.
Did the program work as expected? Great, now you can either delete this print line or put a # in front of it (This will turn the line red) to tell Python to ignore it. I often do little tests like this as I go along to ensure my code is working as intended.
Stage 2 - Getting a dictionary
The second thing we said we needed was to get a list of real words. To help with this I have a file called DictionaryE.txt. This is basically a list of words in a text file. You will need to download this and save it in the same folder as your WordChecker.py program.
Lets write this section as a function. A function is a piece of code which we can call whenever we need to. This was probably the first function I ever wrote!
So how do we write a function? The first thing we need to know is where should we write the function. Well as in real life you need to know how to do something before doing it, and Python works the same way. So we write our function at the top of the program.
The first line of the function is giving it its title, and letting the function know if you are passing any variables into it. For this function we are not, so there is nothing inside the brackets. So type
Note the colon after the brackets.
Did you download the file DictionaryE.txt which contains a whole list of words? We plan to use this as our dictionary. So the next line in the function is.
dictionaryOpen = open('DictionaryE.txt','r')
Note there is a tab (4 spaces) before dictionaryOpen. Remember this whitespace is important for Python. It is an unusual thing for programming languages, and even people who are seasoned programmers find the whitespace a little odd to begin with. However after using it for a while it starts to make perfect sense!
So we have opened the DictionaryE file, and now we need to read the contents.
dictionary = dictionaryOpen.read().split()
Again we are still in the function so the line needs to be indented as the line above.This line does two things:
- It reads the file
- It splits each word in the file into a list. This makes it easier for us to play around with later on.
and then as we have gone to the effort of writing a function to determine the dictionary, lets return the dictionary we have found to the main program.
Remember all the lines inside the function should have a tab (4 spaces) before you start typing.
Ok so that's our function written. We now need to call the function. We know our function returns the dictionary, so lets store whatever is returned from the getDictionary() function into a variable called dictionary.
So after the inputtedWord line type
dictionary = getDictionary()
Wow. Halfway through and we have only written 7 lines of code. Pretty impressive huh?
Stage 3 - Comparing words with dictionary.
Next we need to compare the word we have typed with the words in the dictionary. Wouldn't it be easy if we could just ask if the word we inputted is in the dictionary?
Well we can, see I told you python was easy! We can use a simple if statement to check this.
if inputtedWord in dictionary:
Now we have done the check, we can report if its a success or failure.
Stage 4 - Report Success / Failure
If it is True that the inputted word is in the dictionary, we can finish off our if statement, by asking python to act on the information.
Lets use a print statement to report back to the user if the word was in the dictionary.
Remember as we are in the if statement to have a tab (4 spaces) before our next line.
print inputtedWord, "is a word"
This line prints whatever is stored in inputtedWord, which should be the word we typed in and then the text which is in speech marks. You can either use " " or ' ' for speechmarks.
But what if the inputted word is not in the dictionary? Well rather than say nothing we should report this back also. So lets finish off our if statement with an else statement. Anything that is not covered by the if statement is covered in the else statement.
print inputtedWord, "is not a word"
And that should be all we need so lets look at the full program.
There we go. All that remains is to run the program. To do this you need to press F5 which will save the file at the same time. Although I hope you have been saving as you go along.
I would just like to mention the whitespace or tabs before a line. It may look confusing at first, but it really does help you structure your program, and mean it is easier to read when you come back to your program after a few weeks or months.
Why not set yourselves the challenge of modifying this program a little? Perhaps you can create a cross word solver that takes a word with some blanks and returns all possible words. What about an anagram solver?
These are the two programs I tackled next. Don't worry if you struggle with these, as I will cover both of them in a future blog!
In the mean time keep practising!