Reputation: 731
In the following example I have data contained in a dictionary. After applying some code, the correct output should be the value 905. My current code, however, produces the value 1296, which is incorrect.
The variable non_udacity_enollments appears fine, since it gives the same output as the practice code I am learning from.
The code below is an adaptation of the practice code I am learning from.
non_udacity_enrollments=[]
non_udacity_enrollments[:] = [v for v in enrollments if v.get('is_udacity')=='False']
paid_students = {}
for enrollment in non_udacity_enrollments:
if (not enrollment['is_canceled'] or
enrollment['days_to_cancel'] > 7):
account_key = enrollment['account_key']
enrollment_date = enrollment['join_date']
if (account_key not in paid_students or
enrollment_date > paid_students[account_key]):
paid_students[account_key] = enrollment_date
len(paid_students)
I am trying to understand where it is that I am going wrong. Thanks.
As requested, here's the output of print enrollments[0]
{u'status': u'canceled', u'is_udacity': u'True', u'is_canceled': u'True', u'join_date': u'2014-11-10', u'account_key': u'448', u'cancel_date': u'2015-01-14', u'days_to_cancel': u'65'}
And the output of print paid_students
{u'199': u'2015-05-30', u'593': u'2015-04-02', u'1200': u'2015-03-04', u'1175': u'2015-04-02', u'1269': u'2015-08-21', u'1268': u'2015-04-06', u'1256': u'2015-07-18', u'669': u'2015-05-12', u'1257': u'2015-07-09', u'1145': u'2015-04-04', u'344': u'2015-01-11', u'345': u'2015-01-07', u'346': u'2014-12-08', u'347': u'2015-04-05', u'340': u'2015-04-01', u'341': u'2015-05-10', u'342': u'2014-12-05', u'343': u'2014-12-07', u'810': u'2014-11-10', u'919': u'2014-11-10', u'812': u'2015-07-09', u'813': u'2015-07-11', u'348': u'2015-03-05', u'349': u'2015-04-0....
Upvotes: 0
Views: 147
Reputation: 9359
the value of is_canceled
is string, not boolean, so you can do not enrollment['is_canceled']
, because that will be always true.
The same with the value of days_to_cancel
- you are comparing unicode value with integer, that will also give you strange result.
So the condition should be something like this:
if (enrollment['is_canceled'] == 'False' or int(enrollment['days_to_cancel']) > 7):
Upvotes: 1