Please send questions to
st10@humboldt.edu .
* see examples in lect06.py, playing around with reading
if-statements
* so: things are now more ... INTERESTING with conditionals.
* need to IDENTIFY cases involved, in designing solutions
* NEW design recipe step:
Data analysis and Data definition
* see p. 35 of HtDP packet (Figure 6)
* before the examples part (at the latest!)
"Inspect the problem statement for distinct
situations"
* enumerate all possible situations,
* text suggests drawing a number line, identifying
intervals -- that CAN be useful!
* and, then, you'll want at least ONE example PER
distinct situation;
(and one for each boundary, IF applicable)
* note that you could almost give the skeleton
of your function at that part:
def four_case_problem(param1, param2):
if (param1 ... param2 ...):
return ...
elif (... param1 ... param2 ...):
return ...
elif (... param1 ... param2 ...):
return ...
else:
return ...
* then you can FILL THIS IN as you go...
def four_case_problem(param1, param2):
if (param1 > 2 * param2):
return sqrt(param1)
elif (... param1 ... param2 ...):
return False
elif (... param1 ... param2 ...):
return False
else:
return False
* see interest_rate example in lect06.py...
* SECTION 5 - functions with side-effects!
* PIECE #1 - local variables
* so far, we've had parameters (parameter variables),
and named constants (named constant variables)
* a LOCAL VARIABLE is one declared WITHIN, or local
to, something;
inside of a function,
inside of a Python module (.py file)
inside of a python interpreter session
* you declare it by giving it a value by assignment
my_local_var = expr
* now, my_local_var has "meaning", or SCOPE, throughout
the rest of the something it is declared in ---
the rest of that function body,
the rest of that function module (.py file),
the rest of the python interpreter session
SMALL_LIMIT = 10
def min_if_small_else_max(val1, val2):
min_val = min(val1, val2)
if (min_val < SMALL_LIMIT):
return min_val
else:
return max(val1, val2)
* AND: some variables were MEANT to be changed...
* piece #2: the assignment statement
if you use = <--- ASSIGNMENT
... with a variable that ALREADY exists,
it CHANGES the value of that variable;
val = 1
val = 2 <--- val has been CHANGED to 2
* LHS - the part to the left of the assignment operator
RHS - the part to the right of the assignment operator
* in an assignment statement, the value of the
expression on the RHS becomes the new value of the
variable on the LHS
* the thing on the LHS HAS to be able to be given a
new value!!
LECTURE EXERCISE:
count = 0
count = count + 1
#1. what is count now?
#2. WRITE the value of val1, val2, and val3 AFTER the
following 4 statements have been done:
val1 = 0
val2 = val1 + 5
val3 = val2 + val1
val1 = val1 + 1