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

Ivan Avery Frey ivan.avery.frey at gmail.com
Fri Jun 24 23:18:05 UTC 2011


> 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."
     print "Take one down and pass it around,", (i-1), "bottles of beer on the 
wall.\n"

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

----End of Program------

Line 3 of my program should not have been wrapped. My current version of 
Thunderbird doesn't appear to offer a way to turn wordwrap off.

I used the lyrics of the song from this site:
http://99-bottles-of-beer.net/lyrics.html

I added the few lines after the loop for precision. "1 bottles of beer" is just 
bad English.

> 2. What does range(0, 10, -1) give?  (Try it.)
>    Any idea why?
>
Gives the empty set. I would have thought it would have given either an infinite 
loop or a syntax error, so I suspect this is a language convention.

3. Length of Lesson is ok.

4. Print first five numbers as words.

for i in range(5):
	j=(i+1)
	if j==5:
		print "five"
	elif j==4:
		print "four"
	elif j==3:
		print "three"
	elif j==2:
		print "two"
	else:
		print "one"


More information about the Courses mailing list