@@ -17,21 +17,16 @@ def enrollment_stats(list_of_universities):
17
17
return total_students , total_tuition
18
18
19
19
20
- def mean (my_list ):
21
- if len (my_list ) == 0 :
22
- return "The list is empty"
23
- list_sum = 0
24
- for i in range (len (my_list )):
25
- list_sum += float (my_list [i ])
26
- return int (list_sum / len (my_list ))
27
-
28
-
29
- def median (my_list ):
30
- sorts = sorted (my_list )
31
- length = len (sorts )
20
+ def mean (values ):
21
+ return sum (values ) / len (values )
22
+
23
+
24
+ def median (values ):
25
+ values .sort ()
26
+ length = len (values )
32
27
if not length % 2 :
33
- return ( sorts [ int (length / 2 )] + sorts [ int ( length / 2 - 1 )]) / 2.0
34
- return sorts [int (length / 2 )]
28
+ return values [ (length - 1 ) / 2 ]
29
+ return values [int (length / 2 )]
35
30
36
31
37
32
universities = [
@@ -47,12 +42,12 @@ def median(my_list):
47
42
totals = enrollment_stats (universities )
48
43
49
44
print ("\n " )
50
- print ("*****" * 5 )
51
- print (f"Total students: { sum (totals [0 ])} " )
52
- print (f"Total tuition: $ { sum (totals [1 ])} " )
53
- print (f"\n Student mean: { mean (totals [0 ])} " )
54
- print (f"Student median: { median (totals [0 ])} " )
55
- print (f"\n Tuition mean: $ { mean (totals [1 ])} " )
56
- print (f"Tuition median: $ { median (totals [1 ])} " )
57
- print ("*****" * 5 )
45
+ print ("*****" * 6 )
46
+ print (f"Total students: { sum (totals [0 ]):, } " )
47
+ print (f"Total tuition: $ { sum (totals [1 ]):, } " )
48
+ print (f"\n Student mean: { mean (totals [0 ]):,.2f } " )
49
+ print (f"Student median: { median (totals [0 ]):, } " )
50
+ print (f"\n Tuition mean: $ { mean (totals [1 ]):,.2f } " )
51
+ print (f"Tuition median: $ { median (totals [1 ]):, } " )
52
+ print ("*****" * 6 )
58
53
print ("\n " )
0 commit comments