sep 30
Homework comments :
- a**b vs a^b
- import sys; print sys.path ; import os; print os.getcwd()
- ... both on the questions page.
- Dylan's laser.py
Next assignment (chapter 6) is up.
discuss functions
Back to encode / decode, this time with functions .
- What should go in a function?
- What are the tests?
Class exercise :
- First remember how the program works.
- Then take 5 min to decide on some functions.
- Interface vs Implementation
Ideas :
- top-down vs bottom-up programming
- test-driven programming
Look at futval_func_example.py. For each function:
- inputs?
- outputs?
- side effects?
- tests?
- docs?
default values
def do_something(size, color="white"):
# code goes here
print "size, color = %i, %s" % (size, color)
do_something(3, "blue") # this is OK
do_something(4) # also OK; color defaults to "white"
do_something() # *not* OK; 0 args gives error
Args with defaults may only go on the *end* of the argument list ... otherwise the later ones (required) imply that the earlier ones are also required.
Note that this is a python-specific syntax.
multiple return values
def get_stuff():
return 1, "hey"
# And now use it
a, b = get_stuff()
# "Tuples" in python
other bells and whistles
Less used, and can be safely ignored on a first pass through python.
But for those who like the tricky corners...
def two_or_more_args(a, b, *c):
print "a = '" + str(a) + "'"
print "b = '" + str(b) + "'"
print "c = '" + str(c) + "'"
# and now call it.
two_or_more_args(3, "hey") # a=3, b="hey", c=[]
two_or_more_args(3, "hey", 4, "there") # a=3, b="hey", c=[4, "there"]
depending on time
... start "conditionals" (chapters 7 and 8):
value = input("What is the number? (1 to 10) ")
if value < 1 or value > 10 :
print "Oops: your number was too big or too small."
print "Your number is %i" % number
Discuss flowcharts.
Discuss comparison operators.
Coming: boolean variables.