Formative Assessment: Scripts
Although we provide access to answers for this exercise, we want you to submit the codes you generate via Moodle, so that we can provide feedback. You should avoid looking at the answers before you submit your work. This submitted work does not count towards your course assessment, it is purely to allow us to provide some rapid feedback to you on how you are doing. You will need to put together a few elements from the notes so far to do all parts of this practical, but you should all be capable of doing it well. Pay attention to writing tidy code, with useful, clear comments and document strings.
Exercise 1
-
Create a Python code in a file called
work/count.py
that does the following:- define a function
count(istop)
that prints out numbers from 0 toistop
(inclusive) on the same line. Your function should test that the variableistop
is an integer, and if not, try to convert it to one (hint: it might well be a string when you pass it fromsys.argv
below). - define a function
main(vlist)
that loops over each item in the listvlist
and sends it tocount(...)
- calls
main(vlist)
if the file is run as a Python script withvlist
being all arguments aftersys.argv[0]
on the script command line - show a test of the script working
-
has plentiful commenting and document strings
-
As a test, when you run the script:
%run work/count.py 4 5
you would expect to get a response of the form:
0 1 2 3 4 0 1 2 3 4 5
- define a function