# Help With Python

Roy Smith roy at panix.com
Wed Jan 26 11:42:43 EST 2005

```In article <mailman.1356.1106754931.22381.python-list at python.org>,
Judi Keplar <judi-keplar at charter.net> wrote:
>
>I am currently taking a course to learn Python and was looking for
>some help.  I need to write a Python statement to print a comma-
>separated repetition of the word, "Spam", written 511 times ("Spam,
>Spam, =85 Spam").
>
>Can anybody help me get started?  I am completely new to programming!

Well, I'll point you in a couple of good directions.

The first direction would be to approach this as a traditional
procedural programming problem.  You need some kind of loop that can
exectute a print statement 511 times.  In Python, that looks something
like:

for i in range(511):
print "spam"

Almost immediately, the question that comes to mind is where, exactly,
does the loop start and stop?  For example, you might expect that

for i in range(5):
print i

would print the numbers 1 through 5, but it really prints 0 through
4.  This kind of "off by one" thing is a very common common issue in
writing loops in almost any programming language.  It's a common
enough issue that a whole class of bugs is named after it, known as
"fencepost errors".  So, left as an exercise for the reader
(i.e. you), is to figure out if "for i in range(511)" really does the
loop 511 times, or maybe 510 or 512?

Next, you'll notice that the first loop I wrote prints one "spam" on
each line.  The way you described the problem, you want all the spams
on one big long line, with commas between each one.  The way you get
Python to not advance to the next line is to end the print statement
with a comma, like this:

for i in range(511):
print "spam",

Notice that there's something tricky going on here; the comma doesn't
get printed, it's just a way of telling the print statement, "don't go
on to the next line".  To really get it to print a comma, you need
something like:

for i in range(511):
print "spam,",

The first comma is inside the quoted string, so it gets printed.  The
second one is the one that says "don't go to the next line".

You're still not quite done, but I've given you enough hints.  Go play
with that, see what you get, and see if you can figure out what
problems you still need to fix.

I said I'd point you in a couple of good directions, and the second
one is to look up how the "join()" string method works.  It's really
cool, and solves your problem in a different way.  But, first I think
you should master the straight-forward way.

Lastly, the really cool Pythonic way would be to get a bunch of
Vikings into a restaurant and give them all breakfast menus.  The only
problem there is you'd have trouble handing all those Vikings in with