By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,865 Members | 1,717 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,865 IT Pros & Developers. It's quick & easy.

Help With Python

P: n/a

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, … Spam").

Can anybody help me get started? I am completely new to programming!

Thanks in advance!

Jul 18 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a
Am Wed, 26 Jan 2005 15:55:28 +0000 schrieb Judi Keplar:

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, … Spam").

Can anybody help me get started? I am completely new to programming!


Hi,

# This way, there is a comma after the last:
import sys
for i in range(511):
sys.stdout.write("Spam, ")

# No comma at the end:
mylist=[]
for i in range(511):
mylist.append("Spam")
print ", ".join(mylist)

Thomas

--
Thomas Güttler, http://www.thomas-guettler.de/
Jul 18 '05 #2

P: n/a
Judi Keplar schrieb:
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, … Spam").

Can anybody help me get started? I am completely new to programming!


Online:

- http://www.python.org/moin/BeginnersGuide (Beginner, Advanced)
- http://www.freenetpages.co.uk/hp/alan.gauld/ (Beginner)
- http://docs.python.org/tut/tut.html (Beginner)
- http://diveintopython.org/ (Advanced)

Books (Look for most recent editions):

- Mark Lutz, Learning Python (Beginner)
- Alex Martelli, Python in a Nutshell (Beginner, Advanced)
- Frederik Lundh, Python Standard Library (Beginner, Advanced)
- Alex Martelli, Python Cookbook (Beginner, Advanced)
- Mark Pilgrim Dive Into Python (Advanced)

--
-------------------------------------------------------------------
Peter Maas, M+R Infosysteme, D-52070 Aachen, Tel +49-241-93878-0
E-mail 'cGV0ZXIubWFhc0BtcGx1c3IuZGU=\n'.decode('base64')
-------------------------------------------------------------------
Jul 18 '05 #3

P: n/a
Judi Keplar 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, … Spam").

Can anybody help me get started? I am completely new to programming!

Thanks in advance!

print "Spam, " * 510 + "Spam"


Will McGugan
Jul 18 '05 #4

P: n/a
>>> print "Spam, " * 510 + "Spam"
Or if you have 2.4..
print ", ".join( "Spam" for _ in xrange( 511 ) )


Although, the replys with links will ultimately be more helpful!

Will McGugan
Jul 18 '05 #5

P: n/a
Py>>> print "Spam, " * 115
Spam, Spam, Spam, Spam, Spam, ................. Spam,

Multiplies (repeats) the string 115 times.

To eliminate the last ", " (that is, a comma followed by space), you
can do it using the slice notation and say:
Py>>> print ("Spam, " * 115) [:-2]
Spam, Spam, Spam, Spam, Spam, ................., Spam

The [:-2] means that you asking Python to print everything from the
beginning to the last but two characters of the string. You can also
write this as [0:-2]

[or [0:length_of_spam - 2] where length_of_spam = len("Spam, " * 115)]

Since you are starting off, please read and follow the tuturial that
available with your Python installation or on the Python.org site at
http://docs.python.org/tut/tut.html

Thank you,
--Kartic

Jul 18 '05 #6

P: n/a
Thomas Guettler wrote:
Am Wed, 26 Jan 2005 15:55:28 +0000 schrieb Judi Keplar:

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, � Spam").

Can anybody help me get started? I am completely new to programming!

Hi,

# This way, there is a comma after the last:
import sys
for i in range(511):
sys.stdout.write("Spam, ")

# No comma at the end:
mylist=[]
for i in range(511):
mylist.append("Spam")
print ", ".join(mylist)


# also no comma at the end, using a list comprehension
print ", ".join(["Spam" for _ in xrange(511)])

# also no comma at the end, using a generator expresssion (Python 2.4)
print ", ".join("Spam" for _ in xrange(511))

Steve
Jul 18 '05 #7

P: n/a
Thomas Guettler wrote:
# No comma at the end:
mylist=[]
for i in range(511):
mylist.append("Spam")
or just
mylist = ["Spam"] * 511

Kent
print ", ".join(mylist)

Thomas

Jul 18 '05 #8

P: n/a
In article <ma***************************************@python. org>,
Judi Keplar <ju*********@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
your assignment.
Jul 18 '05 #9

P: n/a
Peter Maas wrote:
Can anybody help me get started? I am completely new to programming!
Online:

- http://www.python.org/moin/BeginnersGuide (Beginner, Advanced)
- http://www.freenetpages.co.uk/hp/alan.gauld/ (Beginner)
- http://docs.python.org/tut/tut.html (Beginner)
- http://diveintopython.org/ (Advanced)
also:

http://www.byteofpython.info/
Books (Look for most recent editions):

- Mark Lutz, Learning Python (Beginner)
- Alex Martelli, Python in a Nutshell (Beginner, Advanced)
- Frederik Lundh, Python Standard Library (Beginner, Advanced)
on-line here: http://effbot.org/librarybook
on-line here: http://effbot.org/zone/librarybook.htm (pdf version)
- Alex Martelli, Python Cookbook (Beginner, Advanced)
- Mark Pilgrim Dive Into Python (Advanced)


on-line here: http://diveintopython.org

</F>

Jul 18 '05 #10

P: n/a

Here's my Monty Pythonic answer:

## cut here
class Viking():

def __init__():
pass

def order():
return 'Spam'

# this is one viking making one order repeated 511 times. if you want
# 511 vikings making seperate orders, you'll have to write a loop.
v = Viking()
orders = [ v.order() ] * 511

print ', '.join(orders)
## cut here

With no apologies to Eric Idle,

Nick
--
# sigmask || 0.2 || 20030107 || public domain || feed this to a python
print reduce(lambda x,y:x+chr(ord(y)-1),' Ojdl!Wbshjti!=obwAcboefstobudi/psh?')
Jul 18 '05 #11

P: n/a
Nick Vargish wrote:
Here's my Monty Pythonic answer:

## cut here
class Viking():

def __init__():
pass

def order():
return 'Spam'

# this is one viking making one order repeated 511 times. if you want
# 511 vikings making seperate orders, you'll have to write a loop.
v = Viking()
orders = [ v.order() ] * 511

print ', '.join(orders)


No need to write a loop:

py> class Viking(object):
.... def order(self):
.... return 'Spam'
....
py> v = Viking()
py> orders = [v.order()] * 7
py> ', '.join(orders)
'Spam, Spam, Spam, Spam, Spam, Spam, Spam'
py> orders = [Viking().order()] * 7
py> ', '.join(orders)
'Spam, Spam, Spam, Spam, Spam, Spam, Spam'

Steve
Jul 18 '05 #12

P: n/a
Nick Vargish wrote:
Here's my Monty Pythonic answer:

## cut here
class Viking():

def __init__():
pass

def order():
return 'Spam'

# this is one viking making one order repeated 511 times. if you want
# 511 vikings making seperate orders, you'll have to write a loop.
v = Viking()
orders = [ v.order() ] * 511

print ', '.join(orders)
## cut here

With no apologies to Eric Idle,

Nick
--
# sigmask || 0.2 || 20030107 || public domain || feed this to a python print reduce(lambda x,y:x+chr(ord(y)-1),'

Ojdl!Wbshjti!=obwAcboefstobudi/psh?')

Not to be nit-picky, but you got some issues here ;-). Never forget
the importance of "self"!

#######################
class Viking:
.. def __init__(self):
.. pass
.. def sing(self):
.. return 'Spam'
v = Viking()
spam_song = [ v.sing() ] * 511
print ', '.join(spam_song)

Jul 18 '05 #13

P: n/a
"Matt" <ma*************@countrywide.com> writes:
Not to be nit-picky, but you got some issues here ;-). Never forget
the importance of "self"!


Teach me to post untested code. *hangs head*

Nick

--
# sigmask || 0.2 || 20030107 || public domain || feed this to a python
print reduce(lambda x,y:x+chr(ord(y)-1),' Ojdl!Wbshjti!=obwAcboefstobudi/psh?')
Jul 18 '05 #14

P: n/a
Steven Bethard <st************@gmail.com> wrote:
Nick Vargish wrote:
# this is one viking making one order repeated 511 times. if you want
# 511 vikings making seperate orders, you'll have to write a loop.


No need to write a loop:

py> class Viking(object):
... def order(self):
... return 'Spam'
...
py> v = Viking()
py> orders = [v.order()] * 7
py> ', '.join(orders)
'Spam, Spam, Spam, Spam, Spam, Spam, Spam'
py> orders = [Viking().order()] * 7
py> ', '.join(orders)
'Spam, Spam, Spam, Spam, Spam, Spam, Spam'


Thats still one Viking making 7 orders surely?

Eg
vikings = [Viking()] * 7
vikings[0] is vikings[1] True

whereas
vikings = [Viking() for _ in range(7)]
vikings[0] is vikings[1] False

So you want this...
orders = [ Viking().order() for _ in range(7) ]


--
Nick Craig-Wood <ni**@craig-wood.com> -- http://www.craig-wood.com/nick
Jul 18 '05 #15

P: n/a
Nick Craig-Wood wrote:
Steven Bethard <st************@gmail.com> wrote:
py> orders = [Viking().order()] * 7
py> ', '.join(orders)
'Spam, Spam, Spam, Spam, Spam, Spam, Spam'


Thats still one Viking making 7 orders surely?

So you want this...
orders = [ Viking().order() for _ in range(7) ]


Right, right. Thanks for the correction! This is why I never use the *
operator on lists. ;)

Steve
Jul 18 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.