Skip to content

Commit 96ab671

Browse files
Update 01 Average Numbers
1 parent 40049ac commit 96ab671

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

1 Python/labs/01 Average Numbers

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,31 @@
1+
Lab 00: Average Numbers
12

3+
We're going to average a list of numbers. Start with the following list, iterate through it, keeping a 'running sum', then divide that sum by the number of elements in that list. Remember len will give you the length of a list.
4+
5+
The code below shows how to loop through an array, and prints the elements one at a time.
6+
7+
nums = [5, 0, 8, 3, 4, 1, 6]
8+
9+
# loop over the elements
10+
for num in nums:
11+
print(num)
12+
13+
# loop over the indices
14+
for i in range(len(nums)):
15+
print(nums[i])
16+
17+
Version 2
18+
19+
Ask the user to enter the numbers one at a time, putting them into a list. If the user enters 'done', then calculate and display the average. The following code demonstrates how to add an element to the end of a list.
20+
21+
nums = []
22+
nums.append(5)
23+
print(nums)
24+
25+
Below is an example input/output:
26+
27+
> enter a number, or 'done': 5
28+
> enter a number, or 'done': 3
29+
> enter a number, or 'done': 4
30+
> enter a number, or 'done': done
31+
average: 4

0 commit comments

Comments
 (0)