Class 24 — Wednesday October 23
Module implementation and usage
Practice makes perfect — Is more than a platitude — Take it to your heart
Look both ways
Agenda
- Function-based chrestomathics
World series
- Program astros.py
- Prints the Astros will win the world series
Astros will win the World Series
- Program nationals.py
- Prints the Nationals will win the world series
Nationals will win the World Series
- For a chance extra credit, you have until 2:15 PM to upload who will be winner of the world series.
- Cannot modify existing code of
astros.py
ornationals.py
.
- Cannot ask me or any course personnel for insights.
Downloads
To do
- Look over artifacts
- Look at Hands on Python
- Look at function epistle
Module olio.py
Function
voting_age()
- Does not take any parameters. Returns an integer indicating how old you need to be to vote. For example, the following code segment initializes both x and y to the integer
18
.
x = olio.voting_age()
y = olio.voting_age()
Function
has_blanks( s )
- Has one parameter
s
. Returns whethers
contains at least one blank character; i.e., returnsTrue
ifs
contains a blank, and returnsFalse
otherwise. For example, the following code segment initializesb1
andb2
toTrue
andFalse
respectively.
x = 'CS 1112'
y = 'the_aarvark_said_arf_arf'
b1 = olio.has_blanks( x )
b2 = olio.has_blanks( y )
Function
great_seal()
- Does not have any parameters and does not return a value. The function prints text from the Great Seal of the United States, i.e., E Pluribus Unum.
- The code segment
olio.great_seal( )
print()
olio.great_seal( )
print()
produces as output
E Pluribus Unum
E Pluribus Unum
Function
a_ing( n )
- Has one parameter
n
. The function does not perform a return, instead it printsn
lines of output. The first line prints a single'a'
, the second line prints'aa'
, the third line prints'aaa'
, and so on. The string operator*
should prove useful. For example usage, the code segment
olio.a_ing( 5 )
print()
olio.a_ing( 1 )
print()
olio.a_ing( 3 )
produces as output
a
aa
aaa
aaaa
aaaaa
a
a
aa
aaa
Module uva.py
- Let's implement a worthwhile module with four functions
- A function with no parameters that does not perform an explicit return;
- A function with parameter(s) that does not perform an explicit return;
- A function with no parameters that returns something of value;
- A function with parameter(s) that returns something of value;
- Program rotunda.py
- Let's implement along the way a simple tester of module
uva.py
What's next
- Look at Hands on Python
- Two www.python.org resources should be examined now and also re-examined later.
- The Python Tutorial includes a section on functions. I recommend before going there to thoroughly review the discussion presented here.
- Python enhancement proposal 008 (PEP 008) includes recommended practices for naming modules, functions, and variables. The recommendations suggest that function and variable names are as a rule written in _snakecase; that is, lower-case words joined by underscores. Module names are expected to be short, lower-case, and no separation between words. I will add that function names are often action or verb phrases; e.g.,
input_integer()
.
© 2019 Jim Cohoon | Resources from previous semesters are available. |