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

Kay Nettle pkn at cs.utexas.edu
Sat Jun 25 12:03:41 UTC 2011


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


for i in range(99, 1, -1):
  print '%i bottles of beer on the wall, %i bottles of beer' % (i, i)
  if i == 2:
      print 'Take one down and pass it around, 1 bottle of beer on the
wall.'
  else:
      print 'Take one down and pass it around, %i bottles of beer on the
wall.' % (i-1)

print """1 bottle of beer on the wall, 1 bottle of beer.
Take one down and pass it around, no more bottles of beer on the wall. 
No more bottles of beer on the wall, no more bottles of beer.
Go to the store and buy some more, 99 bottles of beer on the wall."""


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

It gives an empty list.  I would have guess it would have 
given you an error.

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

It could be a little longer.

 >4. Write a loop using range() that prints out the first five numbers
 >   as words:
 >   one
 >   two
 >   three
 >   four
 >   five


numbers=('one', 'two', 'three', 'four', 'five')
for i in range(len(numbers)):
        print numbers[i]




More information about the Courses mailing list