[Courses] [python] Lesson 2: Loops, if, and beer

Monique Y. Mudama monique at bounceswoosh.org
Sun Jun 26 23:55:47 UTC 2011


On Fri, Jun 24 at 13:31, Akkana Peck penned:
> Today we're going to do some real programming:
> "for" loops and variables.

Yay!

I'm coming to this party a little late - sorry about that!  I'm
running python 2.6.6 on debian unstable.

1) Bottles of beer!  I modified it a bit to match the song as I
learned it.
#
def choose_word(num_bottles):
        if num_bottles == 1:
                return "bottle"
        else:
                return "bottles"

for num_bottles in range(99, 0, -1):
        print num_bottles, choose_word(num_bottles), "of beer on the wall," , num_bottles, choose_word(num_bottles), "of beer"
        print "Take one down, pass it around," , (num_bottles-1) , choose_word(num_bottles-1), "of beer on the wall!"
#

2) range(0, 10, -1) returns an empty list.  This seems right to me - if you add -1 to 0, you get -1, which is outside of the set of numbers between 0 and 10, so there's no matching output.

3) Probably about right - the program took me a while, but that's because I am allergic to duplicate code and didn't immediately see how I wanted to deal with "beer" vs. "beers". 

4) (after some googling)
#
numbers = {
        1 : "one",
        2 : "two",
        3 : "three",
        4 : "four",
        5 : "five"
}

for number in range(1,6):
        print numbers[number];
#

I've avoided reading the other replies until I finished the lesson, so now I'll go read the other responses and see how others approached it.

-- 
monique


More information about the Courses mailing list