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

Desiree Jenkins buffarama at gmail.com
Sat Jun 25 04:05:50 UTC 2011


On Fri, Jun 24, 2011 at 3:31 PM, Akkana Peck <akkana at shallowsky.com> wrote:
> ===================== 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, 0, -1) :
	if i == 1 :
		print i,"bottle of beer on the wall,",i,"bottle of beer,"
	else :
		print i,"bottles of beer on the wall,",i,"bottles of beer,"

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

[]

>   Any idea why?

the first two numbers specify from 0 to 10, forwards, and the -1
specifies to count backwards, you can't do both at the same time.

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

I thought it was a little short, but as others have said, I've done
programming before, so it may be someone else's perfect pace.

> 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"
	elif i == 5 :
		print "five"

Desiree Jenkins


More information about the Courses mailing list