Sum All Grid Values
Intermediate · Write the code · from Nested Loops: Patterns, Performance, and When to Avoid Them
From the lesson: Nested Loops: Patterns, Performance, and When to Avoid Them
Think about a spreadsheet. It has rows and columns. To read every cell, you go through each row, and for each row, you go through each column. That's two loops - one inside the other. That's a nested loop.
Read the lessonA grid of numbers is given. Use nested loops to calculate and print the sum of all values in the grid.
Expected output:
45grid = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
# Calculate the sum of all values
More Nested Loops exercises
- Print a RectangleBeginner
- Predict the OutputIntermediate
- Number TriangleIntermediate
- All Outfit CombinationsIntermediate
- Fix the Coordinate GridIntermediate
All Python exercises · 50 practice problems · Control Flow quiz