-2

So I'm pretty new to python and it's our first language in college. I am having a hard time putting the "Row #x:" in my code:

x = int(input("Enter the number of rows: "))
y = int(input("Enter the number of columns: "))

n=1
for i in range(x):
for j in range(y):
print(n, end=" ")
n = n+1
print()

Output:

Enter the number of rows: 3

Enter the number of columns: 3

1 2 3

4 5 6

7 8 9

So this is how it's supposed to look like if the "Row #x:" if its included:

Enter the number of rows: 3

Enter the number of columns: 3

Row #1: 1 2 3

Row #2: 4 5 6

Row #3: 7 8 9

The number of rows depends on how many rows the user has inputted. Anything to share with me on how am I supposed to do it?

Vivek Lele
  • 109
  • 4
chrlsssd
  • 1
  • 1
  • "I am having a hard time putting the `"Row #x:"` in my code". What code have you tried to include `"Row #x:"` in your output? – blhsing Sep 30 '22 at 02:20
  • i found this out, print("Row #",i+1,":", end=" ") but now instead of Row #1: it became, Row # 1 : – chrlsssd Sep 30 '22 at 02:25
  • Then your question is a duplicate of: https://stackoverflow.com/questions/29528767/how-to-print-without-spaces-in-python-3 – blhsing Sep 30 '22 at 02:28
  • bro thank you so much for the help, can you please post the answer so i can confirm it? – chrlsssd Sep 30 '22 at 02:43

2 Answers2

1

you could do something like this:

for i in range(x):
    print(f'Row #{i + 1}:', end=' ') # what you were missing to print the row numbers
    for j in range(y):
        print(n, end=' ')
        n = n+1
    print()

This is using f-strings, the i variable is +1 because you are starting at a 0 index

Andrew Ryan
  • 1,489
  • 3
  • 15
  • 21
-1

Below is the code

row = int(input("Enter number of row: "))
column = int(input("Enter number of column: "))

count = 1
for num in range(1, row*column+1):
    print(num, end=" ")
    if num == count*column:
        print()
        count += 1
Vivek Lele
  • 109
  • 4
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 04 '22 at 01:22