因此,我必须向标准输出写入100号,但在一行中仅输入10号,我编写的代码几乎是完美的,但输出如下:
23456789101
5378566145
8353464573
7596745634
4352362356
2342345346
2553463221
9873422358
8223552233
578942378
并且有代码:
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
我怎样才能做到完美?
最佳答案
您需要从1开始并转到UPTO + 1
:
for i in xrange(1, UPTO + 1):
更改代码后,您可以:
In [18]: main()
3989867912
0729456107
3457245171
4564003409
3400380373
1638374598
5290288898
6348789359
4628854868
4172212396
您还可以将print作为从
__future__
导入的功能使用,以简化代码: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()
关于python - python换行符10个元素,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36321667/