WEEK02: numbers and strings
---------------------------------------------------------------
M: review, accumulator pattern, main(), debug-as-you-go
LAB1: due Saturday night
REVIEW:
$ cat blastoff.py
"""
countdown program to show use of for loop and range
J. Knerr
Spring 2011
"""
# ---------------------------------------- #
def main():
"""get start, count down to 1"""
start = int(raw_input("\ncountdown start: "))
print " "
# counting down from user input
for i in range(start,0,-1):
print ". " * i + str(i)
print"blastoff!! \n"
# ---------------------------------------- #
main()
-----------------------------------------------------------------
$ cat WHAT_I_EXPECT_FROM_YOU
What I expect from you this semester:
------------------------------------
* start the labs early, finish all the labs
* read the book/web page/assignments
* come to class
* if you have to miss a class, let me know why
* do your own work
* keep up
* if you are falling behind, ask for help
* if you don't understand something, ask
* come talk to me or Frances, before it becomes a problem
* work hard and do your best
-----------------------------------------------------------------
accumulator pattern:
- suppose I have a large bucket and I ask everyone in the class
to put all of their money in the bucket? This is the idea of
an ACCUMULATOR: start with an "empty" variable (the bucket) and
repeatedly add to what's in the bucket
- in python (and other languages) you can add to what's already
stored in a variable like this:
x = x + 1
this looks odd if it's a mathematical statement, but it's not -- it's
an assignment. Take whatever is stored in the variable x, add one to
it, then assign the result back to the variable x
- here's how you would sum up 10 numbers the user enters:
sum = 0
for i in range(10):
usernum = raw_input("enter a number: ")
sum = sum + float(usernum)
print "The sum of those numbers is: ", sum
# ------------------------------------------------------------------ #
WHAT DOES THIS PROGRAM DO???
"""
Calculate the Gamma function of n, where
Gamma(n) = (n - 1)! = (n-1)*(n-2)*...*2*1
J. Knerr
Spring 2011
"""
# ---------------------------------------------- #
def main():
"""get n, calculate G(n), output results"""
print "Gamma(n) calculator...please enter n\n"
n = input("n: ")
result = 1
for i in range(2,n):
result = result * i # looks like an ACCUMULATOR!!!
# use print formatting to output results
print "\nGamma(%d) = %d" % (n, result)
# ---------------------------------------------- #
main()
HOW IS IT DIFFERENT FROM THIS PROGRAM???
n = input("n: ")
x = 1
for i in range(2,n):
x = x * i
print x
- they actually do the same thing, but one has nice comments
and good variable names, and will be much easier to understand
at a later date, should you ever need to revise it
# ------------------------------------------------------------------ #
DEBUG-AS-YOU-GO: type in a line or two of python code, then
test it out (don't type in your whole program, then try to run it).
Programs are much easier to debug if you just add one or two
lines, then test them to make sure they work.
# ------------------------------------------------------------------ #
YOUR TURN: can you write this program???
$ python grade_ave.py
please enter # of grades: 5
grade 1: 99
grade 2: 87
grade 3: 75
grade 4: 100
grade 5: 93.5
the sum of those grades is: 454.50
the ave of those grades is: 90.90
# ------------------------------------------------------------------ #