[Courses] [python] Lesson 2: Loops, if, and beer
dondashguitar at gmail.com
Sat Jun 25 03:57:14 UTC 2011
On 06/24/2011 03:31 PM, Akkana Peck wrote:
> Today we're going to do some real programming:~
> 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"
print i, "bottles of beer on the wall", i, "bottles of beer"
Note: This taught me the importance of consistent indentation because
it didn't work the first few times I ran it and I had a tough time
figuring out why.
> 2. What does range(0, 10, -1) give? (Try it.)
> Any idea why?
Because when you count backwards from 0 there's no return.
> 3. How was the length of this lesson? Too much, too little or just right?
This lesson was WAY too short (I'm having a blast with it). :)
> 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:
for i in range(1, 6) :
if i == 1 :
elif i == 2 :
elif i == 3 :
elif i == 4 :
Thank you, thank you, thank you!
A proud user of Debian Squeeze w/KDE 4.4.5
More information about the Courses