-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlist_mean.py
More file actions
29 lines (23 loc) · 864 Bytes
/
list_mean.py
File metadata and controls
29 lines (23 loc) · 864 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# The mean of a set of numbers is the sum of the numbers divided by the
# number of numbers. Write a procedure, mean, which takes a list of numbers
# as its input and return the mean of the numbers in the list.
# Hint: You will need to work out how to make your division into decimal
# division instead of integer division. You get decimal division if any of
# the numbers involved are decimals.
def list_mean(l):
sum_of_list = float(sum(l))
numbers_in_list = float(len(l))
if numbers_in_list == 0:
return "The list is empty"
else:
return float(sum_of_list / numbers_in_list)
print list_mean([1,2,3,4])
#>>> 2.5
print list_mean([1,3,4,5,2])
#>>> 3.0
print list_mean([])
#>>> ??? You decide. If you decide it should give an error, comment
# out the print line above to prevent your code from being graded as
# incorrect.
print list_mean([2])
#>>> 2.0