I'm trying to complete the assignment (Quiz 21) described below for the following course:
https://classroom.udacity.com/courses/ud170/lessons/5430778793/concepts/53961386480923
The first code fragment is the one I wrote, which outputs the wrong lengths for the lists. The second code fragment is the suggested solution. Although I understand the suggested solution, I cannot work out why the code I wrote outputs different values.
I am trying to create two lists of engagement records, one for students who have passed a specific course identified by the lesson key (there are two values because the key changed in the relevant period) and one for students who took the course but didn't pass. Project_submissions
is a list of dictionaries (a table) and contains information on individual students' (identified by their account keys) courses (identified by lesson keys). The information on students' engagements is contained within paid_engagement_in_first_week
, a list of dictionaries containing information on factors such as how many times they visited the site to study etc. The two tables link students by their account keys.
Here are some examples of what paid_submissions
and paid engagement
give:
print(paid_submissions[0])
print(paid_engagement[0])
OrderedDict([('creation_date', datetime.datetime(2015, 1, 14, 0, 0)), ('completion_date', datetime.datetime(2015, 1, 16, 0, 0)), ('assigned_rating', 'UNGRADED'), ('account_key', '256'), ('lesson_key', '3176718735'), ('processing_state', 'EVALUATED')])
OrderedDict([('utc_date', datetime.datetime(2015, 1, 9, 0, 0)), ('num_courses_visited', 1), ('total_minutes_visited', 11.6793745), ('lessons_completed', 0), ('projects_completed', 0), ('account_key', '0'), ('has_visited', 1)])
And here is the script:
######################################
# 11 #
######################################
## Create two lists of engagement data for paid students in the first week.
## The first list should contain data for students who eventually pass the
## subway project, and the second list should contain data for students
## who do not.
subway_project_lesson_keys = ['746169184', '3176718735']
passing_engagement=[]
non_passing_engagement=[]
for dct in paid_submissions:
if (dct['lesson_key'] in subway_project_lesson_keys) and (dct['assigned_rating']=='PASSED'or dct['assigned_rating']=='DISTINCTION'):
for dct2 in paid_engagement_in_first_week:
if dct2['account_key']==dct['account_key']:
passing_engagement.append(dct2)
elif (dct['lesson_key'] in subway_project_lesson_keys) and not (dct['assigned_rating']=='PASSED'or dct['assigned_rating']=='DISTINCTION'):
for dct2 in paid_engagement_in_first_week:
if dct2['account_key']==dct['account_key']:
non_passing_engagement.append(dct2)
print(len(passing_engagement))
print(len(non_passing_engagement))
#
subway_project_lesson_keys = ['746169184', '3176718735']
pass_subway_project = set()
for submission in paid_submissions:
project = submission['lesson_key']
rating = submission['assigned_rating']
if ((project in subway_project_lesson_keys) and
(rating == 'PASSED' or rating == 'DISTINCTION')):
pass_subway_project.add(submission['account_key'])
len(pass_subway_project)
passing_engagement = []
non_passing_engagement = []
for engagement_record in paid_engagement_in_first_week:
if engagement_record['account_key'] in pass_subway_project:
passing_engagement.append(engagement_record)
else:
non_passing_engagement.append(engagement_record)
print(len(passing_engagement))
print(len(non_passing_engagement))