NCEA Compatibility
Module: as91883 Develop a computer program
Requirements: Documenting the program with comments
Go to topModule: as91883 Develop a computer program
Requirements: Documenting the program with comments
Go to topOn completion of this section you will know
If we go through the code in Listing 1 below with the words of the above heading on our minds we may notice the following:
This three-part division is not peculiar to our programme alone or to the way it was designed. Just about all programmes fall into these three divisions.
The division where we get data from the user is known as the Input section because the programme accepts raw input data from the user. In our case, the user keyed in the raw data at the keyboard in response to prompts from the user. As we move further into programming we will see other ways of getting raw input data:
The division where the programme uses the raw data in order to produce further values is known as the Processing section of the programme. This occurs once all of the raw data has been received and is usually invisible to users. In our case the processing section uses the raw input data, i.e. the values for hours and rate to calculate values for the gross, tax and net pay. By the end of the processing part all of those values are stored in variables inside the computer’s memory and invisible to the users – hence the necessity of the next section.
The Output section takes the values calculated in the processing section and makes them visible to the users. In our case it simply prints the values of the gross, tax, superannuation contribution and net on to the console.
Again, as we progress further with our programming, other methods of output will become familiar to us:
Now that we know how to work out the different sections of our programme and where each section begins and ends, it would be useful to make them more easily discernable to a newcomer who is not familiar with the programme layout. The way to do this is through comments.
We have been using comments since we started this course although the comments have been very limited. The first line of all of our programmes started with the ‘#’ followed by the name of the file our programme was stored in.
If a line starts with the ‘#’ symbol, it is a signal to Python to ignore this line because it is not a programming command but an explanation or a piece of information for the person reading the programme.
In the earlier part of this text we analysed the programme in order to determine which groups of lines comprised the Input, Process and Output. Here we shall use comments in order to indicate the beginning and end of each of those sections. This means that it will be easier for a stranger reading the programme to determine where each section of the programme begins and ends.
Listing 2 below shows a commented version of the programme. Lines 3 and 7 are comments indicating the start and end of the Input section while line pairs 9, 16 and 18, 23 indicate the beginning and end of the Input, Process and Output sections.
Also notice that each comment line, apart from line 1, ends in a group of ‘*’ characters. This is in order to make the comment line more visible to a viewer. You can replace those ‘*’ characters with any other character or group of characters you wish, if you feel that it will make your comment more visible or you can leave them out. Remember that they will be ignored by the programme so it is entirely up to you what you put into them.
Although we have used only one-line comments in our example here, a comment can take up as many lines as you wish. However each line of the comment must begin with the ‘#’ symbol.
Among the major programming languages, Python is unique in that it uses indentation to delineate different levels within the programme, in the same way we use indentation to distinguish different levels within normal text.
Examining the code in Listing 2 above we notice that lines 3 to 10 are not indented but placed against the left margin. This indicates that they are top level commands, all of equal importance. For this reason the Python interpreter will execute each one of them in turn.
Line 12, however, is indented one tab space. This indicates that it is the body of the if construct at line 11. This line will only get executed of the condition at line 11 is trur, i.e. the value stored in the variable floatGross is less than 500, otherwise it is skipped.
Line 13 is again not indented since since the else is the alternative to the if at line 11. This indicates that if the condition at line 11 is false, i.e. if the value of floatGross has a value of 500 or more then line 12 is skipped and control passes to line 13, which then passes control to line 14. Again line 14 is indented since it will only execute if the condition at line 11 is false.
The rest of the code, i.e. line 15 and lines 19 to 21 are not indented, indicating that they will always execute.
Here we looked at three aspects of Python coding:
Modify the code you created for Assignment Part 3 so that the Input, Process and Output sections of it are commented in a fashion similar to Listing 2 above