Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
7550 views
ubuntu2004
1
# TODO Make the month_names list from the months module available
2
from months import month_names
3
4
# TODO Make the sort_by_month function from the months module
5
# available as sort_by_month_name
6
7
# Make the matplotlib module available
8
import matplotlib
9
10
# Set up rendering of matplotlib so that it doesn't try to
11
# display the plot
12
matplotlib.use('Agg')
13
14
# Make the plotting functions of matplotlib available
15
import matplotlib.pyplot as plt
16
17
# Ask the user for how many people are in the class, storing the
18
# number entered into an integer variable named number_of_students
19
number_of_students = int(raw_input('How many people are in the class? '))
20
21
# Initialize a variable named number_entered to keep of how many people we have entered
22
number_entered = 0
23
24
# Initialize an empty dictionary named month_counts
25
month_counts = dict()
26
people_by_month = dict()
27
28
# TODO Set the value associated with each month in months_names to 0 in the
29
# dictionary named month_counts
30
for month in month_names:
31
month_counts[month] = 0
32
people_by_month[month] = list()
33
34
# As long as there are more people
35
# TODO: Fill in a comparison of number_entered with number_of_students
36
while number_entered < number_of_students:
37
# Ask for the next person's birthdate
38
line = raw_input('Enter name and birthday, separated by comma: ')
39
40
# TODO Split up the line into parts, separated by the , character, storing the
41
# result into a list named name_and_birthday
42
name_and_birthday = line.split(',')
43
print "The name is ", name_and_birthday[0], 'and birthday is', name_and_birthday[1]
44
45
# TODO Strip off any spaces from the birthday portion of name_and_birthday, then
46
# split based on a space, storing the two parts into a list named
47
# month_and_day
48
month_and_day = name_and_birthday[1].strip(' ').split(' ')
49
50
# TODO Set the variable "month" to be the 3 letter abbreivation, which is
51
# one of the elements in the list named month_and_day
52
month = month_and_day[0]
53
54
# TODO Increment the number of people whose birthdays we've entered, which
55
# is in the variable number_entered
56
number_entered += 1
57
58
# TODO Increment the value in the month_count dictionary for this month
59
month_counts[month] += 1
60
people_by_month[month].append(name_and_birthday[0])
61
62
# Get out the x_values, which are the month names
63
x_values = month_names
64
65
# Create a list of the y-values from the month_counts dictionary, in the same order as the values in the months_names
66
# list
67
y_values = list()
68
for month in month_names:
69
y_values.append(month_counts[month])
70
71
# Create a plot object with formatting specifying no line,
72
# just circles, for the values in x_values and y_values
73
plt.plot(x_values, y_values, 'ro')
74
75
# Save that plot to the file months-dictionary.png
76
plt.savefig('months-dictionary-wednesday-3.png')
77
78
for month in month_names:
79
if len(people_by_month[month]) > 0:
80
print month, people_by_month[month]
81
else:
82
print month, ": no one born in", month
83