Please send questions to
st10@humboldt.edu .
* miscellaneous projected notes, CIS 130 Week 4 Lecture, 2-5-07
What's happening this next week...
------------------------------------
today: start discussing Section 4 of the packet
Wednesday 2-7: review for Exam #1, which covers the first 3 sections
of the reading packet (through and including HW #2)
(YES this is a lab exercise)
Monday 2-12: Exam #1
Wednesday 2-14: lab on Section 4 material, HW #3 comes out
-----------------------------------------------
* turns out there are 4 basic structures of programming...
* and one of them is called branch/conditional
* what if you wanted to compute the pre-tax wages for a
person's work?
* contract:
# contract: net_wages: number number -> number
* then header;
# contract: net_wages: number number -> number
#
# purpose: determine the net wages for a employee when
# given his/her <hourly_wage> and the number
# of <hours> worked; but a worker who works
# more than 40 hours gets overtime of
# time-and-a-half for all hours over 40
#
# examples: net_wages(12, 20) should be 240
# net_wages(12, 40) should be 480
# net_wages(12, 60) should be 840
OVERTIME_START = 40
OVERTIME_FACTOR = 1.5
def net_wages(hourly_wage, hours):
... but BEFORE I can write the body, I would like to know
how I might write a CONDITIONAL - something only done
sometimes, say when a CONDITION holds;
... and before I can write a CONDITIONAL, I need to be able
to write a CONDITION.
* to indicate that something holds or not --- is True or False --
many languages have the idea of a Boolean (named after George Boole),
a type with exactly two possible values: True and False.
* in Python, the Boolean type has the name bool
... and the two possible bool literals are True and False
* and just like numeric expresions can be combined with arithmetic
operators to build other numeric expressions,
there are operators whose results are bool/Boolean
* relational operators
< > <= >= ==
(think of == as "is equal to")
these take numeric operands, return a bool result
* Boolean operators
and or not
these take bool operands, return a bool result
and: True ONLY if BOTH operands are True
* T and T -> T
T and F -> F
F and T -> F
F and F -> F
or: True if EITHER or both operands are True
* T and T -> T
T and F -> T
F and T -> T
F and F -> F
not: it is a PREFIX operator (it goes BEFORE a boolean expr)
and it returns the opposite of that expression
not T -> F
not F -> T
* in lecture exercise: work in pairs, put BOTH names on paper:
write the VALUE of each boolean expression
1. (1 + 3) == (5 - 1)
2. not (5 < 6)
3. (50 < 60) and (60 < 70)
4. not (True or False)
5. 40 <= 40
* NOTE: these are BINARY, statements like 40 < 30 < 60 DON'T work
like you think --- use AND to compare ranges:
(40 < 30) and (30 < 60)
* and if an expression can have a bool value, then a function
can return a bool value...
consider is_overtime --- whose goal is to say whether
a number of hours worked is eligible to receive overtime
pay
# contract: is_overtime: number -> bool
# purpose: returns true if <hours> is more than
# OVERTIME_START, and returns false otherwise
# examples: is_overtime(20) == False
# is_overtime(40) == False
# is_overtime(60) == True
def is_overtime(hours):
return hours > OVERTIME_START
* INTERVALS will be discussed after exam 1 ---
* BUT, now that we can write conditions, what's one of the
Python conditional statements:
if <condition1>: # condition1: a bool expression
<statement1>
[elif <condition2>:
<statement2>
elif <condition3>:
<statement3>
...]
[else:
<statement4>]
def net_wages(hourly_wage, hours):
if is_overtime(hours):
return ((OVERTIME_START * hourly_wage) +
(hours - OVERTIME_START) *
(hourly_wage * OVERTIME_FACTOR))
else:
return hourly_wage * hours
* note: how can I test if a real number is "close enough"?
a useful kluge: abs( actual - desired ) < .001
( abs( net_wages(12.5, 10) - 125 ) < .001 ) == True