counter = 1
numbers = int(input("Enter a number between 1 and 99: "))
column = int(input("How many columns would you like? "))
output_string = ""
col_counter = 0
while (counter <= numbers):
output_string += str(counter)+" "
counter += 1
col_counter += 1
if(col_counter == column):
print(output_string)
output_string=""
col_counter = 0
print(output_string)
如何将行号添加到此代码中?我的代码正是我想要的方式。。。只想输出
第1行:12345
第2行:678910
第3行:11121314
您只需添加一个变量来表示行号,然后打印如下:
counter = 1
numbers = int(input("Enter a number between 1 and 99: "))
column = int(input("How many columns would you like? "))
output_string = ""
row = 1
col_counter = 0
while (counter <= numbers):
output_string += str(counter)+" "
counter += 1
col_counter += 1
if(col_counter == column):
print('Row'+str(row)+':'+output_string)
output_string=""
col_counter = 0
row+=1
print(output_string)
更新:我错过了最后一行没有得到正确的输出,以防你的数字不能被整除。所以,把最后一行改成这样:
if output_string != '':
print('Row'+str(row)+':'+output_string)