Reputation: 55
I want my program to output rows*columns in that many rows and columns. For instance, table(2,3) would output
1 2 3
4 5 6
So far I have
def table(rows, columns):
print(*range(1,rows*columns+1))
It prints rows*columns, but I don't know how to print it in a tabular format. Thank you for the help.
Upvotes: 0
Views: 97
Reputation: 756
You can add a for loop before your print statement like this:
def table(rows, columns):
for i in range(rows):
print(*range(1+i*columns, 1+(i+1)*columns))
This way you print a new line for each iteration of the loop and the range increases by the number of columns.
Upvotes: 2