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

Darrin Goodman darrin.goodman at gmail.com
Mon Jun 27 16:49:38 UTC 2011


>
> ---------- Forwarded message ----------
> > From: Akkana Peck <akkana at shallowsky.com>
> > To: courses at linuxchix.org
> > Date: Fri, 24 Jun 2011 13:31:11 -0700
> > Subject: [Courses] [python] Lesson 2: Loops, if, and beer
> >
> > ===================== Homework =======================
> >
> > 1. Your first programming assignment!
> >   Write a program for the old "bottles of beer" song. It should print
> >   something like:
> >   99 bottles of beer on the wall
> >   98 bottles of beer on the wall
> >   97 bottles of beer on the wall
> >   ... all the way down to 1.
> >
> >   Extra credit: make it print the number twice, e.g.
> >   99 bottles of beer on the wall, 99 bottles of beer
> >
>
>
# This program will produce output for the song, "99 Bottles of Beer On The
Wall"
# and provides an introduction to using the for loop:

# for i = 99-1, decrement by 1 each time)
for i in range(99, 0, -1) :
    if i == 1 :
        print i, "bottles of beer on the wall,", i, "bottles of beer..."
        print "take one down, and pass it around..."
        print "there ain't no more bottles of beer on the wall"
        print " "
    else :
        print i, "bottles of beer on the wall,", i, "bottles of beer..."
        print "take one down, and pass it around..."
        print i-1, "bottles of beer on the wall"
        print " "


> >
> > 2. What does range(0, 10, -1) give?  (Try it.)
> >   Any idea why?
> >
>

This range decrements i by 1 from 0 to 9.  Since i will never reach 9, no
action will be taken (such as print output).

>
> > 3. How was the length of this lesson? Too much, too little or just right?
> >
>

I like that the lessons are short so that you have time to gain an
understanding of the topic, but I wonder if covering a little bit more might
keep us occupied throughout the week.  I feel like I am chomping at the bit
for the next lesson but will have to wait until Friday...  Perhaps as the
programming becomes a bit more complex, the homeworks might require a bit
more time for us to complete them?  I sure appreciate what you are doing
here and after a few previous failed attempts to learn Python, I'm excited
to have another opportunity.

Also, in regard to Lesson 1's question about platform and python version:
 - CrunchBang Linux (Debian Squeeze); Python 2.6.6
 - Xubuntu 11.04; 2.7.1+


> >
> > Problem 4 is optional -- it's a little harder, but give it a try,
> > and if you don't get it, take a look at the answers other students
> > post. But try it on your own first, if you can.
> >
> > 4. Write a loop using range() that prints out the first five numbers
> >   as words:
> >   one
> >   two
> >   three
> >   four
> >   five
> >
>
>
for i in range(1, 6) :
    if i == 1 :
        print "one"
    elif i == 2 :
        print "two"
    elif i == 3 :
        print "three"
    elif i == 4 :
        print "four"
    else :
        print "five"

Thanks so much!  Can't wait for the next one!

-Darrin


> _______________________________________________
> Courses mailing list
> Courses at linuxchix.org
> http://mailman.linuxchix.org/mailman/listinfo/courses
>


More information about the Courses mailing list