2017-04-27 42 views
-1

作为一组循环练习的一部分,我应该编写一个读取两个整数X和Y的程序。作为输出,程序打印所有从1到Y的整数,组织成X数字出现在每一行上。在同一行中打印数字

所以我应该在某处使用whilefor。我至今是:

代码

n_per_line = int(input('Numbers per line: ')) 
upperbound = int(input('Upper bound: ')) 

i = 1 
n = 1 

while i <= upperbound: 
    while n <= n_per_line: 
     print(i, end=' ') 
     n += 1 
     i += 1 

然而,这仅仅打印出一个输出线。例如,

Numbers per line: 3 
Upper bound: 4 

gives 1 2 3 in a single line. And 

Numbers per line: 2 
Upper bound: 4 

gives 1 2 in a single line. 
+0

你有任何的代码? –

+2

请在问题中键入您的代码作为文本,而不是显示它的图形。从文本编辑器复制并粘贴到您的问题中,突出显示代码,然后单击“{}”按钮,以便将其格式化为文本。 –

回答

1

你需要把打印新线

while con1: 
    while con2: 
     ... 
     print(...,end=" ") #this print will put everything in the line 
     ... 
    print() #make a new line, so any following print use it 
0
a=int(input()) 
b=int(input()) 
for i in range(1,b+1): 
    print(i,end=' ') 
    if i%a==0:print()