The aim of this week's lesson (and the later Now Code 2) is to give you an opportunity to practice your programming and good working practice. You will attempt a number of exercises which will involve working in pairs to design, implement and test your solutions.
So far the example we have undertaken have been relatively short due to time constraints. This means that we have generally not spent too long planning our code, however for anything but the simplest programs this can be a mistake. Writing pseduocode or flowcharts can help us to understand what our program needs to do. When we have more significant programming challenges it can also help us to break down the code into functions and architect the different components that we will need to include.
As an example let's consider the mean
function that we have considered in previous weeks. This functionality can be summarised as follows:
Function: mean(list)
Takes list passed from calling function
If list is empty, return warning message: it is not possible to take the mean of an empty list
If list contains non-numerical values, return warning message: it is only possible to average numerical values
Otherwise:
sum list
count items in list
return mean: sum/count
Now because of the natural language of Python we can readily turn this into executable code:
def mean(sample):
'''
Takes a list of numbers, sample
and returns the mean.
'''
assert len(sample) != 0, "Unable to take the mean of an empty list"
for value in sample:
assert isinstance(value,int) or isinstance(value,float), "Value in list is not a number."
sample_mean = sum(sample) / len(sample)
return sample_mean
Before starting any of the exercises try to contruct pseudocode for your solution.
In professional development practices such as code review and pair programming are used to ensure quality, develop understanding and provide mentoring. During these exercises you are encouraged to work with a neighbour. Develop your pseudocode together to plan how you are going to tackle the problem. When you start coding one of your will drive or pilot, explaining what you are doing when appropriate, and the other will navigate or guide, as necessary. Both should understand what and why you are doing what you are doing at all times.
Swap frequently, and at least for each part/subexercise, but when you are pilot only ever work on your own copy of the code. In order to do this, one of you will have to create your own remote copy of the repository, e.g. on github and make your partner a collaborator.
When you clone the repository for each exercise the original remote will be set as origin. To check this and change the remote follow these commands:
% git remote -v
origin https://github.com/arc-bath/<exercise>.git (fetch)
origin https://github.com/arc-bath/<exercise>.git (push)
% git remote remove origin
create a new empty remote repository on you github account called rainfall
. Then add it to your local repository and push it to the remote, by following the instructions on github or with the following:
% git add remote origin https://github.com/<your_username>/<exercise>.git
% git push -u origin master