[Courses] [python] Lesson 2

Annie Desaulniers elyana_ab at hotmail.com
Fri Jul 15 13:00:59 UTC 2011


On 06/24/2011 11:46 PM, courses-request at linuxchix.org 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
>
> 2. What does range(0, 10, -1) give?  (Try it.)
>    Any idea why?
>
> 3. How was the length of this lesson? Too much, too little or just right?
>
> 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
>
1. 

strbottle="bottles of beer"

for i in range (99,0,-1) :
    if i == 1 :
        strbottle="bottle of beer"
    print i, strbottle, "on the wall,", i, strbottle

2.
the first value in range is where it starts counting, the second value
is where it stops and the third value is the increment
so if 0 is incremented by -1, it will never reach 10, from what I
understand of the python documentation on the "for" statement : the
sequence created is empty.
http://docs.python.org/reference/compound_stmts.html#the-for-statement
3.
I would like more :)
4.
list = ["one","two","three","four","five"]
for i in range(0,5) :
    print list[i]






More information about the Courses mailing list