logo CodeStepByStep logo

print_grid

Language/Type: Python basics loops parameters

Write a function named print_grid that accepts two integer parameters rows and cols. The output is a comma-separated grid of numbers where the first parameter (rows) represents the number of rows of the grid and the second parameter (cols) represents the number of columns. The numbers count up from 1 to (rows x cols). The output are displayed in column-major order, meaning that the numbers shown increase sequentially down each column and wrap to the top of the next column to the right once the bottom of the current column is reached.

Assume that rows and cols are greater than 0. Here are some example calls to your function and their expected results:

call print_grid(3, 6)  print_grid(5, 3)  print_grid(4, 1)  print_grid(1, 3)
output
1, 4, 7, 10, 13, 16
2, 5, 8, 11, 14, 17
3, 6, 9, 12, 15, 18
1, 6, 11
2, 7, 12
3, 8, 13
4, 9, 14
5, 10, 15
1
2
3
4
1, 2, 3
Function: Write a Python function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.