def mean(num_list):
'''
Function to calculate the mean of a list of numbers
Usage: mean(list_of_numbers)
Checks that length of list is not 0, else raises assertion error.
Checks that all items are ints of floats, else raises assertion error.
returns sum(list_of_numbers)/len(list_of_numbers)
'''
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
def main():
'''
Simple check of mean(num_list):
calls mean on:
numbers = [1, 2, 3, 4, 5],
returning the mean, 3.0
nonumbers = [], empty list
which causes an assertion error
word_and_numbers = [1, 2, 3, 4, "apple"], string in list
which would raise assertion error is executed
'''
numbers = [1, 2, 3, 4, 5]
print(mean(numbers))
nonumbers = []
print(mean(nonumbers))
word_and_numbers = [1, 2, 3, 4, "apple"]
print( mean(word_and_numbers) )
if __name__ == '__main__':
main()
Check that your documentation is working correctly by importing the script in ipython3 and running help(my_script)
.
Version Control!
In the directory containing your new script initialise a new git repository and add the script to it.
% git init
Initialised empty Git repository in /u/q/rjg20/intro-testing/my_testing/.git/
% git add mean.py
% git commit -m "Intiailised repository, a little late but with code, test and documentation"
[master (root-commit) c01f726] Intiailised repository, a little late but with code, test and documentation
1 files changed, 29 insertions(+)
create mode 100644 mean.py
Don’t forget to keep the repository upto date as we progress this lesson!