Format the ouput in my python code

S

sl33k

I am printing the numbers from 1 to 100. In that, I want to display
multiples of 3,5 and of both as mulof3, mul0f5 and mulof3and5
respectively.

I am getting the output I want but I would like to format the output
to print only 10 number per line. How do I go about doing this?

for i in range(1, 101):
if i % 3 == 0:
if i % 5 == 0:
print 'mulof3and5',
else:
print 'mulof3',
elif i % 5 == 0:
print 'mulof5',
else:
print i
 
D

Dave Angel

I am printing the numbers from 1 to 100. In that, I want to display
multiples of 3,5 and of both as mulof3, mul0f5 and mulof3and5
respectively.

I am getting the output I want but I would like to format the output
to print only 10 number per line. How do I go about doing this?

for i in range(1, 101):
if i % 3 == 0:
if i % 5 == 0:
print 'mulof3and5',
else:
print 'mulof3',
elif i % 5 == 0:
print 'mulof5',
else:
print i
Change that loop into a generator, having it return values rather than
printing them. Then call that generator in a for-loop, something like:

for index, val in enumerate(mygen):
print val,
if not index%10: print
 
D

Dave Angel

Change that loop into a generator, having it return values rather than
printing them. Then call that generator in a for-loop, something like:

for index, val in enumerate(mygen):
print val,
if not index%10: print
Oops. That was untested, and it probably wasn't quite what you wanted.
More likely something like (untested):

for index, val in enumerate(mygen):
print val,
if not ((index+1)%10): print
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top