- College
CUNY School of Professional Studies
- Course-Name
Software Application Programming I
- Course-Code
IS 210
In this assignment, we'll focus on the construction of basic functions. The functions we create will be intentionally simple to make it easier to isolate the task at hand.
The following tasks will either have you interacting with existing files in the assignment repository or creating new ones on the fly. Don't forget to add your interpreter directive, utf-8 encoding, and a short docstring with any new files that you create!
Important
In these exercises, you may, on occasion, come across a task that requres you to research or use a function or method not directly covered by the course text. Since Python is such a large language it would be impossible for the author to have included descriptions of each and every available function which would largely duplicate the offical Python documentation.
A vital skill to successful programming is being comfortable searching for and using official language documentation sources like the Python String Documentation page. Throughout our coursework we will be practicing both the use of the language in practice and the search skills necessary to become functional programmers.
Functions have their own documentation standards which are covered in the Google Python Style Guide. Document the function found here appropriately.
Warning
Neither Lint nor Unit tests will test the quality of your documentation or whether or not it's formatted correctly. You must document each function correctly in order to receive credit.
Hint
Indentation and spacing both matter so pay attention to how you indent the headers and the sections.
Hint
Remember that optional parameters should be indicated as-such in their declaration!
- Document the function in
task_01.py
according to the Google Python Style Guide.
Hint
You can use the help()
function to test your docstring as below.
>>> import task_01
>>> help(task_01.know_what_i_mean)
Calling a function is already something we've done a few times but let's practice it again just to make sure we've connected the dots.
- Open
hamlet.py
to get a sense of what this function does. - Open
task_02.py
and call thehamlet.crazy_math()
math function assigning it the following parameters in order:- 4
- 100000
- 8
- 98
- Assign the returned result to a new global variable named
POSITIONAL
>>> import task_02
>>> print task_02.POSITIONAL
0.00374391674995
Positional parameters may suffice in many cases but most programmers prefer to use keyword arguments. Here you'll call a function with keyword arguments instead of positional parameters.
- Open
hamlet.py
to get a sense of what this function does. - Open
task_03.py
and call thehamlet.crazy_math()
math function assigning it the following parameters by keyword reference:- bananas: 48
- monkeys: 84
- hours: 200000
- Assign the returned result to a new global variable named
KEYWORD
>>> import task_03
>>> print task_03.KEYWORD
0.00879168510437
In this task, you'll be defining a function with three parameters.
- Create a new file names
task_04.py
- Define a new function named
too_many_kittens
that takes three arguments, in order:- kittens, the number of kittens
- litterboxes, the (integer) number of available litterboxes
- catfood, a boolean representing whether or not any catfood exists
In the function return the value of the following comparison statement:
not (litterboxes >= kittens and catfood)
This statement ensures we have at least one litterbox for each kitten and that we have some catfood. It then uses inversion via
not
to answer whether or not we have too many kittens.
Note
Note the spacing of the not
operator. There should always be spacing around all logical operators like and
, not
or or
. Without it, not
would look like a function, eg not()
.
Note
A fun fact of the polymorphic properties of python is the fact that truthiness would allow catfood
to either be a boolean (eg, True
) or some number like 0
or even None
and this would continue to operate in a reasonably sane manner.
>>> too_many_kittens(12, 12, False)
True
>>> too_many_kittens(13, 12, True)
True
>>> too_many_kittens(12, 13, True)
False
Here we'll set a default value in our function definition.
- Create a file named
task_05.py
- Create a new function named
defaults
with two parameters:my_optional
which has a default value of Truemy_required
which is a required param and has no default value
Return the following logical comparison:
my_optional is my_required
>>> defaults(True)
True
>>> defaults(True, False)
False
>>> defaults(False, False)
True
Code must be functional and pass tests before it will be eligible for credit.
Lint tests check your code for syntactic or stylistic errors To execute lint tests against a specific file, simply open a terminal in the same directory as your code repository and type:
$ pylint filename.py
Where filename.py
is the name of the file you wish to lint test.
Unit tests check that your code performs the tested objectives. Unit tests may be executed individually by opening a terminal in the same directory as your code repository and typing:
$ nosetests tests/name_of_test.py
Where name_of_test.py
is the name of the testfile found in the tests
directory of your source code.
All tests may be run simultaneously by executing the runtests.sh
script from the root of your assignment repository. To execute all tests, open a terminal in the same directory as your code repository and type:
$ ./runtests.sh
Your code should be submitted via Blackboard, as a compressed folder containing python files.