Skip to content

Commit 73b9be0

Browse files
authored
Merge pull request #37 from PdxCodeGuild/daniel-lab01-avgNums
Lab1
2 parents 3c97986 + f8514c3 commit 73b9be0

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# ab 00: Average Numbers
2+
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+
# runningSum = 0
9+
10+
# # loop over the elements
11+
# for num in nums:
12+
# runningSum += num
13+
# # print(runningSum)
14+
15+
# avgNum = runningSum / len(nums)
16+
# print(avgNum)
17+
18+
19+
20+
21+
# # Version 2
22+
23+
# # 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.
24+
# from cProfile import run
25+
26+
27+
runningSum = 0
28+
inputCount = 0
29+
numList = []
30+
31+
32+
lab1 = True
33+
while lab1 == True:
34+
enterNum = input("Enter a number or type 'done' when finished: ")
35+
if enterNum != "done":
36+
try:
37+
enterNum = float(enterNum)
38+
numList.append(float(enterNum))
39+
except:
40+
print("Type a number")
41+
else:
42+
lab1 = False
43+
for num in numList:
44+
runningSum += num
45+
avgNum = runningSum / len(numList)
46+
47+
print(f"runningSum = {runningSum}, avgNum = {avgNum}")
48+
print(numList)
49+
50+

0 commit comments

Comments
 (0)