Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #15997 > unrolled thread

Format the ouput in my python code

Started bysl33k <ahsanbagwan@gmail.com>
First post2011-11-21 04:13 -0800
Last post2011-11-21 09:34 -0500
Articles 3 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Format the ouput in my python code sl33k <ahsanbagwan@gmail.com> - 2011-11-21 04:13 -0800
    Re: Format the ouput in my python code Dave Angel <d@davea.name> - 2011-11-21 09:27 -0500
    Re: Format the ouput in my python code Dave Angel <d@davea.name> - 2011-11-21 09:34 -0500

#15997 — Format the ouput in my python code

Fromsl33k <ahsanbagwan@gmail.com>
Date2011-11-21 04:13 -0800
SubjectFormat the ouput in my python code
Message-ID<3f23c6ac-0554-4cb7-bf7c-b98c29721796@z22g2000prd.googlegroups.com>
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

[toc] | [next] | [standalone]


#16010

FromDave Angel <d@davea.name>
Date2011-11-21 09:27 -0500
Message-ID<mailman.2901.1321885680.27778.python-list@python.org>
In reply to#15997
On 11/21/2011 07:13 AM, sl33k wrote:
> 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



-- 

DaveA

[toc] | [prev] | [next] | [standalone]


#16011

FromDave Angel <d@davea.name>
Date2011-11-21 09:34 -0500
Message-ID<mailman.2902.1321886085.27778.python-list@python.org>
In reply to#15997
On 11/21/2011 09:27 AM, Dave Angel wrote:
> On 11/21/2011 07:13 AM, sl33k wrote:
>> 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
>
>
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




-- 

DaveA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web