[Courses] [python] Lesson 2: Loops, if, and beer
bpower at easydns.com
Fri Jun 24 21:24:16 UTC 2011
How embarrassing, I thought I clicked "reply list."
On 06/24/2011 05:23 PM, Brad wrote:
>> 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.
> I'm a show-off, so I made this:
> #! /usr/bin/python
> for i in range(99, 0, -1):
> print i, "bottles of beer on the wall,", i, "bottles of beer!"
> print "Take one down, pass it around,", (i-1), "bottles of
> beer on the wall!"
> Two side questions: Are we going to discuss shebangs, or just run
> everything as "python <script>"? And for indentation, will any
> whitespace at all do? My habit these many years has been to use tab,
> which clearly Python is happy with, but are there limitations in that
>> 99 bottles of beer on the wall, 99 bottles of beer
>> 2. What does range(0, 10, -1) give? (Try it.)
>> Any idea why?
> It's an empty set, since we can't get to 10 from 0 by incementing by
> -1. So it gives us nothing. Is that right?
>> 3. How was the length of this lesson? Too much, too little or just
> I was pleased with it. I would've been happy doing a bit more
> honestly, but I've got experience with Perl so I'm probably not the
> one who should be setting the pace.
>> 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:
> #! /usr/bin/python
> 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!"
> elif i == 5:
> print "Five!"
> print "Oh no!" # Because even when I'm learning I
> refuse to build an if/elif structure without an else.
More information about the Courses