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?
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()