The starter code is meant to print the average of the marks with two decimals, but it has a bug. Find it (step through it in the visualizer) and fix it.
What to build
A corrected copy of the starter code.
Requirements
- Find the bug by stepping through the code.
- Fix it so that the average is correct for every input.
Input
The first line has n (1 to 100). The second line has n marks.
Output
The average with two decimal places.
Constraints
n is from 1 to 100.
Example
Input:
3
70 80 90
Output:
80.00
Why it works
Finding the wrong value by watching it change is faster and more reliable than rereading the code and guessing.
Examples
Example 1 · Input
3 70 80 90
Expected output
80.00
Example 2 · Input
1 55
Expected output
55.00
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Watch which positions i takes in the loop.
Hint 2
Positions start at 0.
Solution
Show one correct solution (try it yourself first)
n = int(input())
marks = [int(x) for x in input().split()]
total = 0
for i in range(n):
total += marks[i]
print(f"{total / n:.2f}")