Kingston Joshua
Kingston Joshua

Reputation: 25

python line break by 10 elements

So I have to write to the standard output 100 number but in a line only put 10 number, I write the code it is almost perfect but the output is like:

23456789101
5378566145 
8353464573 
7596745634 
4352362356 
2342345346 
2553463221 
9873422358 
8223552233 
578942378

and there is the code:

import sys
import random as r

UPTO = 100

def main():
    for i in xrange(UPTO):
        if i % 10 == 0 and i != 0:
            sys.stdout.write(str(r.randint(0, 9)) + '\n')
        else:
            sys.stdout.write(str(r.randint(0, 9))) 
    print

how can I do this perfectly?

Upvotes: 2

Views: 154

Answers (6)

ShadowRanger
ShadowRanger

Reputation: 155683

Fixing your existing code is, as others have noted, just a matter of starting at 1, not 0 (or adding 1 to i when testing the remainder).

But you don't need to reinvent the wheel at all. You can simplify automatic wrapping a lot with textwrap.wrap. Just generate the output data all at once, e.g.:

digits = 100
# Make a 100 long string of digits
alltext = ''.join(str(random.randrange(10)) for _ in range(digits))

# Or to achieve the same effect more efficiently:
alltext = str(random.randrange(10**(digits-1), 10**digits))

then use textwrap.wrap and a loop to print it:

import textwrap

for line in textwrap.wrap(alltext, width=10):
    print line

Which gets (for example):

4961561591
3969872520
1896401684
8608153539
0210068022
3975577783
8321168859
8214023841
8138934528
7739266079

Upvotes: 0

Sci Prog
Sci Prog

Reputation: 2691

For python2

from random import randint
import sys
for i in range(100):
  sys.stdout.write(str(randint(0,9))+("\n" if i%10==9 else ""))

or if you allow the "print" function instead of the print statement

from __future__ import print_function
from random import randint
for i in range(100):
  print(randint(0,9), end="\n" if i%10==9 else "")

Upvotes: 0

Padraic Cunningham
Padraic Cunningham

Reputation: 180542

You need to start at 1 and go to UPTO + 1:

for i in xrange(1, UPTO + 1):

Once you change that your code works:

In [18]: main()
3989867912
0729456107
3457245171
4564003409
3400380373
1638374598
5290288898
6348789359
4628854868
4172212396

You can also use print as a function with an import from __future__ to simplify your code:

from __future__ import print_function

import random as r

UPTO = 100

def main():
    # use a step of 10
    for i in range(0, UPTO, 10):
        # sep="" will leave no spaces between, end="" removes newline
        print(*(r.randint(0, 9) for _ in range(10)), end="", sep="")
        print()

Upvotes: 1

Ben
Ben

Reputation: 6358

I would import the print from python3 and use that:

from __future__ import print_function
import random

UPTO = 100
for i in xrange(100):
    for j in xrange(10):
        print(random.randint(0,9), end='')
    print()

Upvotes: 1

Adam Smith
Adam Smith

Reputation: 54243

Sounds like a good use of the itertools grouper recipe.

import itertools

def grouper(iterable, n, fillvalue=""):
    args = [iter(iterable)] * n
    return itertools.izip_longest(*args, fillvalue=fillvalue)

UPTO = 100

digits = [str(random.randint(0, 9)) for _ in range(UPTO)]

sys.stdout.write("\n".join(map("".join, grouper(digits, 10)))

Upvotes: 0

Mike Müller
Mike Müller

Reputation: 85612

Change your loop in:

for i in xrange(1, UPTO + 1):

Code:

import sys
import random as r

UPTO = 100

def main():
    for i in xrange(1, UPTO + 1):
        if i % 10 == 0 and i != 0:
            sys.stdout.write(str(r.randint(0, 9)) + '\n')
        else:
            sys.stdout.write(str(r.randint(0, 9))) 
    print
main()

Output:

5324707535
0662651201
6774603548
2062356640
2371234722
0295841132
5498111577
0871557117
3062255375
2219008944

Upvotes: 1

Related Questions