[Courses] [python] Lesson 2: Loops, if, and beer
Margot
margot at lawrence1961.f9.co.uk
Sat Jun 25 13:56:42 UTC 2011
On Fri, 24 Jun 2011 13:31:11 -0700
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
>
I don't drink beer, and the way I see it is that learning
programming should teach me to do things that fit my own needs
instead of having to wait for someone else to do something that
might meet their needs better than it meets mine, so...
for i in range(99,1, -1) :
print i, "cups of cold coffee on the desk,", i, "cups of cold
coffee." print "Put one cup out of the way on the floor,", i-1,
"cups of cold coffee on the desk."
for i in range(1,0, -1) :
print "Oops! Floor completely covered, and no more clean
cups!"
> 2. What does range(0, 10, -1) give? (Try it.)
> Any idea why?
>
>>> range(0, 10, -1)
[]
>>>
I think this empty result is because I'm trying to tell it to count
backwards from 0 to 10, which is impossible.
> 3. How was the length of this lesson? Too much, too little or
> just right?
>
Just right for this total beginner - thank you!
> 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"
--
Margot
More information about the Courses
mailing list