Block B

Docstrings, preconditions, and postconditions A docstring is a triple-quoted string that appears right after a function header. Its audience is another program, or maybe you, a few months after you wrote the function. It is a brief reference describing what the function does.

A good way to manage this is to have two sections.

Preconditions These describe anything that shhould be true before the function is called. This can include a description of the number and types of inputs the function should have.

Postconditions These describe anything that should be true after the function is called. Here, describe any return value and any side-effects calling the function produces.


def square(x):
    """preconditon: x is a number
postcondtion: returns the square of the number passed it.
There are not side-effects"""
    return x*x
def greet_customer(name):
    """prec: name is a string
postcondition:  prints "Hello (name)." to the screen"""
    print(f"Hello, {name}")
def greet_customer1(name):
    """prec: name is a string
postcondition:  returns the string "Hello, (name)"""
    return f"Hello, {name}" 
print(square(10))
print(square.__doc__)
greet_customer("Shannon")
print(greet_customer1("Shannon"))

My function assignments will be speced in this way.

Whats up __doc__? The standard functions all have docstrings.

You can also get information a function by calling the help function on it in an interactive session.

Simple if An if requires a predicate, which is a boolean-valued expression. If the predicate evaluates to True, the CODE executes. Otherwise CODE gets skipped.


if predicate:
    CODE

def f(x):
    if x < 0:   #simple if
        x = -x
    return x

This is an absolute value function.

Fork it: if/else The if-else progression looks like this. If the predicate evaluates to True the block TRUE_CODE executes. Otherwise, the block FALSE_CODE executes. Something is guaranteed to happen. This is a two-tined fork.


if predicate:
    TRUE_CODE
else:
    FALSE_CODE

two-tined fork


def pass_NJ_license(score):
    if score >= 80:
        return "PASS"
    else:
        return "FAIL"

x = input("Enter a score:  ")
x = int(x)
print(f"Your result is {pass_NJ_license(x)}")

Having a good tine with if/else/elif You can have as many tines as you want with this construct.


if predicate1:
    CODE_1
elif predicate2:
    CODE_2

    .
    .
    .


elif predicateN:
    CODE_N
else:
    CODE_DEFAULT

Welcome to Dusty's Pub, where attitude reigns king!


def get_age():
    """precondition: None
postconditiion: Queries user for age and returns the result as an integer"""
    age = input("Welcome to dusty's pub.  How old are you?  ")
    return int(age)


age = get_age()
if age < 21:
    print("Git outta hieah, kid!")
elif(age < 65):
    print("Name yer poizon, baw!")
else:
    print("Shall I cash yer soshal, Geezer??")

Let's write a function that returns a month's name in response to its number: 1 → January, 2 → February, etc.

Here's one way.


def monthName(n):
    """precondition: n is an integer, 1 <= n <= 12.
postconditon: returns the month name, 1 is January, 2 is February, etc"""
    if n == 1:
        return "January"
    if n == 2:
        return "February"
    if n == 3:
        return "March"
    if n == 4:
        return "April"
    if n == 5:
        return "May"
    if n == 6:
        return "June"
    if n == 7:
        return "July"
    if n == 8:
        return "August"
    if n == 9:
        return "September"
    if n == 10:
        return "October"
    if n == 11:
        return "November"
    if n == 12:
        return "December"

Just because you have a hammer, the entire world is not a nail!


def monthName(n):
    """precondition: n is an integer, 1 <= n <= 12.
postconditon: returns the month name, 1 is January, 2 is February, etc"""
    names = ["", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
    return  names[n]