Class 24 — Wednesday October 24
Reaching an understanding with functions
Practice makes perfect — Is more than a platitude — Take it to your heart
Look both ways
Agenda
- Another try at functions
Example de jeur — module primal
-
Code: primal.py
Function
is_factor( x, y )
- Parameters
x
andy
are integers. The function returns whethery
is a factor ofx
.
Function
are_relative_primes( x, y )
- Parameters
x
andy
are integers. The function returns whetherx
andy
are relatively prime; i.e., whethery
is not a factor ofx
and vice-versa.
Function
is_prime( x )
- Parameter
x
is an integer. The function returns whetherx
is prime; i.e., its only factors are 1 and itself
-
Slides: PDF
-
Tester: factoid.py
Homework module take_two
-
Code: take_two.py
Function
signum( x )
- Function parameter
x
is a numeric value. The function returns integer -1, 0, or 1 depending respectively whetherx
is negative, zero, or positive. The function does not print any output.
Function
mid( s )
- Function parameter
s
is a string. If the length of strings
is odd, the function returns the middle character ofs
; otherwise, the function returns the two middle characters ofs
. The function does not print any output.
-
Tester: taken.py
To do
- Review artifacts and prior function materials
- Complete homework
© 2019 Jim Cohoon | Resources from previous semesters are available. |