我要求输入(X)并打印1 - 1000.X用于打印每行的X量。我如何实现这一目标?
perLine = int(input("How many numbers per line would you like: "))
for lineVariable in range(1, 1000, 1):
print(lineVariable)
示例输出(5)
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
答案 0 :(得分:1)
perLine = int(input("How many numbers per line would you like: "))
rn = map(str, range(1, 1000, 1))
from itertools import islice
print("\n".join([" ".join(islice(rn, perLine))
for _ in range(1, 1000, perLine)]).rstrip())
答案 1 :(得分:1)
以下是您的要求:
per_line = int(input("How many numbers per line would you like: "))
for number in range(1, 1001):
print(number, end = " " if number % per_line else "\n")
给你以下输出:
How many numbers per line would you like: 10
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
.
.
981 982 983 984 985 986 987 988 989 990
991 992 993 994 995 996 997 998 999 1000
或者对于使用Python的grouper recipe
的版本per_line = int(input("How many numbers per line would you like: "))
for line in zip(*[iter(range(1, 1001))] * per_line):
print(" ".join([str(x) for x in line]))
答案 2 :(得分:0)
仅在" lineVariable"时插入换行符是X的倍数:
perLine = int(input("How many numbers per line would you like: "))
for lineVariable in range(1, 1001):
print(lineVariable, end=" ")
if lineVariable%perLine==0:
print("")
请注意,即使从1开始而不是0,如果要打印1000个值,则必须使用范围(1,1001)。
答案 3 :(得分:-1)
perLine = int(input("How many numbers per line would you like: "))
for lineVariable in range(1, 1000, 1):
print(lineVariable * perLine)