NewPythonLearner
NewPythonLearner

Reputation: 49

Adding row number to each row of my output - I gave up(Not a python pro)

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)

How would I add row numbers to this code? My code is just the way I want... Just want the output to be

Row 1: 12345

Row 2: 678910

Row 3: 11121314

Upvotes: 0

Views: 536

Answers (1)

Sameh
Sameh

Reputation: 86

You can just add a variable to represent the row number and make the print like this:

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)

UPDATE: I missed that the last line is not resulting the correct output in case your numbers are not equally divisible.So, change the last line to be like this:

if output_string != '':
  print('Row'+str(row)+':'+output_string)

Upvotes: 2

Related Questions