[Courses] [python] Lesson 2: Loops, if, and beer
GcX
linuxchix.gcx at gishpuppy.com
Sat Jun 25 08:51:09 UTC 2011
Instructor Peck,
Hope your week was great and thanks for another lesson.
I write this as I conduct your lesson, so my question my be answered later
on.
Here goes:
After doing the first loop, I tried this and it worked (as if you did know
know it would):
for i in range (0,5):
print '???'
I used ??? because it was shorter then Hello, world! and I just wanted to
see what will happen. I understand that range gives you the start and end
points. So if you want to run the loop 5 times, range (1,6) would do it.
However it seemed odd to me.
A question. Is doing this wrong or would/could problems later on (other then
having driving other programmers why I did this :-) ?
I assume it would/could, however I can not see it.
I read a beginning Python book sometime.
I bring this up for a reason. That book thought me that for programming,
attention to detail is important.
If you forget (or add) something, then your program would not work (or will
work in a way that you did not mean). In the book, it would show you what to
type. e.g.,
>>> print "Hello, world!"
You would write it (and if you did it correctly) and the program would work.
Ready for a laugh.
When I saw this:
Run python -- just that, without a filename or any other arguments.
Then when you see the >>> prompt, type:
range(1, 6)
I did the following. At the prompt (i.e., >>>) I typed, "prompt, type:"
I then expected to type the next line of, "range(1,6)"
Python (of course) knew nothing of what I was tying to do and told me so:
SyntaxError: invalid syntax
>>> prompt, type:
I was using IDLE and the colon was highlighted in red. I will not tell you
how long it took me to "debug" this.
I will tell you that is was after I checked my spelling, and making sure
that I placed the comma in the right place. :-0)
When you wrote:
range can do more than count by ones. If you ask for range(0, 10, 2)
it will count two at a time.
At the Python >>> prompt, try typing:
range(10, 2)
[2, 4, 6, 8]
I assume I was to write, "range(0,10, 2)"
If not, then I am at a lost.
HOME WORK----------------------------------------
1:
for i in range(99,0,-1):
print i, 'bottles of beer on the wall'
Extra credit:
b = 'bottles of beer on the wall,'
b2= 'bottles of beer'
for i in range(99,0,-1):
print i,b,i,b2
2:
What does range(0, 10, -1) give? My guess, a count down of 0 to -9
After trying, I got " [ ] "
I assume because Python knows you can not get to 10 by counting backwards
from 0.
3. The length is good, however I could take a little more.
4. It took a some (OK, a lot) of trail and error. Here it is.
n = ['One', 'Two', 'Three', 'Four', 'Five']
for i in range(0,5):
print n[i]
I would like to change my answer in question 3 to: The length is just right.
--
Always Faitful,
GcX
More information about the Courses
mailing list