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?
CodePudding user response:
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
CodePudding user response:
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