Read a table of numbers, then print the sum of each row and finally the total of all numbers.
What to build
A table read from input, processed one row at a time.
Requirements
- Read the table.
- Print the sum of each row.
- Finally, print
Total: T.
Input
The first line has r and c (1 to 20). Each of the next r lines has c whole numbers.
Output
r lines with the row sums, then Total: T.
Constraints
r and c are from 1 to 20.
Example
Input:
2 3
1 2 3
4 5 6
Output:
6
15
Total: 21
Why it works
Adding each row's sum to a running total gives the overall figure with no second loop.
Examples
Example 1 · Input
2 3 1 2 3 4 5 6
Expected output
6 15 Total: 21
Example 2 · Input
1 1 7
Expected output
7 Total: 7
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
grid is a list of rows; sum(row) adds one row.
Solution
Show one correct solution (try it yourself first)
r, c = [int(x) for x in input().split()]
grid = [[int(x) for x in input().split()] for _ in range(r)]
for row in grid:
print(sum(row))
print("Total:", sum(sum(row) for row in grid))