Beginner · For loops · 76 of 264
Average of n numbers
Read n and then n numbers; print their average with two decimals.
Input: n on the first line, then n whole numbers, one per line. Output: the average, two decimals.
Examples
Input
3 1 2 4
Output
2.33
Input
1 7
Output
7.00
Input
4 10 -10 5 6
Output
2.75
One more test checks other inputs when you press Check.
Stuck? The idea behind for loops
A for loop repeats its indented lines once for each item: each letter of a string, each item of a list, or each number of a range.
range(5) is 0 to 4; range(1, 6) is 1 to 5; range(10, 0, -1) counts down from 10 to 1.
total = 0
for i in range(1, 6):
total += i
print(total) # 15Watch for: range stops before its end: range(1, n) leaves out n. A print inside the loop prints every time; outside it (not indented), once at the end.