[Courses] [python] Lesson 2: Loops, if, and beer
Leslie
leslie.brothers at verizon.net
Fri Jun 24 23:18:32 UTC 2011
On Fri, 2011-06-24 at 13:31 -0700, Akkana Peck 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 had to go even further and keep the original lyrics:
#!/usr/bin/python
for i in range(99, 0, -1) :
x = (i-1)
print i," bottles of beer on the wall, ", i," bottles of beer,"
print "Take one down and pass it around, ",x," bottles of beer on the
wall."
print "No bottles left now. Anyone got an idea for another song?"
> 2. What does range(0, 10, -1) give? (Try it.)
> Any idea why?
> I got an empty set. Because "it can't" go from 0 to 10 and subtract one each step?
> 3. How was the length of this lesson? Too much, too little or just right?
#!/usr/bin/python
length = raw_input("Was this lesson too much, too little, or just
right?\n")
print "The lesson was", length+"."
(Extra programming practice for the fun of it! I entered "just right".
For formatting, thanks to Holly Bostick for her note on 6/22!)
>
>
> 4. Write a loop using range() that prints out the first five numbers
> as words:
> one
> two
> three
> four
> five
> ______
#!/usr/bin/python
for i in range(1, 11) :
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"
else :
print i
> _________________________________________
> Courses mailing list
> Courses at linuxchix.org
> http://mailman.linuxchix.org/mailman/listinfo/courses
More information about the Courses
mailing list