[Courses] [python] Lesson 2: Loops, if, and beer
Don Crowder
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"
else :
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.)
>
returns []
> 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:
> 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"
else :
print "five"
Thank you, thank you, thank you!
--
Don Crowder
http://www.don-guitar.com
http://twitter.com/eldergeek
http://don-guitar.blogspot.com/
http://www.freelists.org/list/donspatch
http://www.google.com/profiles/dondashguitar
http://www.facebook.com/people/Don-Crowder/1321324044
A proud user of Debian Squeeze w/KDE 4.4.5
More information about the Courses
mailing list