Hacker News Mission, need help with Step 5

Hi all!
In the Mission of Exploring Hacker News Posts, in the end of Step 5, I’m getting the error:
“Can’t convert ‘int’ object to str implicitly”

My code:
for item in result_list:
date = item[0]
date_str = dt.datetime.strptime(date, date_format)
hour = date_str.strftime("%H")
if hour not in counts_by_hour:
counts_by_hour[hour] = 1
comments_by_hour[hour] = item[1]
else:
counts_by_hour[hour] += 1
comments_by_hour[hour] += item[1]

Have tried with
comments_by_hour[hour] += int(item[1])
Still having error.

Slicing result_list[:5]:
[[‘8/16/2016 9:55’, 6],
[‘11/22/2015 13:43’, 29],
[‘5/2/2016 10:14’, 1],
[‘8/2/2016 14:20’, 3],
[‘10/15/2015 16:38’, 17]] - I do have correct number of comments

Paste output/error here
TypeErrorTraceback (most recent call last)
in ()
8 else:
9 counts_by_hour[hour] += 1
—> 10 comments_by_hour[hour] += item[1]

TypeError: Can’t convert ‘int’ object to str implicitly

And my dictionary comments_by_hour looks like:
{‘00’: ‘///////////////////////////////////////////////////////’,
‘01’: ‘////////////////////////////////////////////////////////////’,
‘02’: ‘//////////////////////////////////////////////////////////’,
‘03’: ‘//////////////////////////////////////////////////////’,
etc

I’d appreciate your help!My file is attached.
[Basics (1).ipynb|attachment]

(upload://onhdxKzrXUlRzeaGjvAxFA1YGYq.ipynb) (16.7 KB)

@biloshitska.k, indentation would have helped readability here but I suspect your issue is from a step before this - when you create ‘result_list’ containing the time and comments.

Hi,
I’ve checked all the code before, seems that the issue was while adding values to the result_list:
result_list.append((created_at,num_comments))

I’ve corrected it with square brackets:
result_list.append([created_at,num_comments])

Now it’s working, Thanks!!

2 Likes