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

Larry Cafiero larry.cafiero at gmail.com
Tue Jun 28 06:21:09 UTC 2011

Late on this one (sorry):

> ===================== 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

I hope this is right:

for i in range(99, 0, -1):
    print i, "bottles of beer on the wall,", i, "bottles of beer,"
    print "Take one down, pass it around,", (i-1), "bottles of beer on
the wall."

[Although I tried to put a space between each verse, but couldn't]

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

It gives two brackets with nothing between them. I think it's because
if you're starting at 0, then -1 wouldn't produce any numbers.

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

The length was fine and I have to apologize for my tardy response. I'm
in the throes of moving.

> 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"
    print "five"

More information about the Courses mailing list