Guided Project: Exploring Hacker News. Wrong Answers Compared to Solution Notebook

Hi everyone. I’m so stuck! My answers are different from the solution notebook and I can’t understand why.

Also the first 5 rows is also different to the solution notebook which is very strange. Please can you spot any errors in my code which can explain why my answers is wrong?

Thank you

Screen Link:

My Code:

# Below prints the first 5 rows of the Hacker News data list of lists

opened_file = open('hacker_news.csv', encoding='utf8')
from csv import reader
read_file = reader(opened_file)
hn = list(read_file)
print(hn[:5])

header = hn[0]
hn_no_header = hn[1:] # Gets rid of the header for prepare data for analysis
print(hn_no_header[:5]) # Checks the header is not present in the first 5 rows of data

ask_posts = []
show_posts = []
other_posts = []

for row in hn_no_header:
    title = row[1]
    title_lower_case = title.lower() # converts title to all lowercase letters
    
    if title_lower_case.startswith('ask hn'):
        ask_posts.append(row)
        
    elif title_lower_case.startswith('show hn'):
            show_posts.append(row)
            
    else:
        other_posts.append(row)
        
ask_posts_length = len(ask_posts)
show_posts_length = len(show_posts)
other_posts_length = len(other_posts)

print("The number of Ask Posts is:",ask_posts_length)
print("\n")
print("The number of Show Posts is:",show_posts_length)
print("\n")
print("The number of Other Posts is:",other_posts_length)
print("\n")

print("The total number of posts is:", len(hn_no_header))
    
total_ask_comments = 0

for row in ask_posts:
    num_comments = int(row[4])
    total_ask_comments += num_comments
    
avg_ask_comments = total_ask_comments / len(ask_posts)
    
template = "The average number of ask comments is {num:.2f}" # :.2f to specify decimal to 2 d.p 
string = template.format(num=avg_ask_comments)
print(string)

total_show_comments = 0

for row in show_posts:
    num_comments = int(row[4])
    total_show_comments += num_comments
    
avg_show_comments = total_show_comments / len(show_posts)
    
template = "The average number of show comments is {num:.2f}" # :.2f to specify decimal to 2 d.p 
string = template.format(num=avg_show_comments)
print(string)

What I expected to happen:

ask post: 1744
show posts: 1162
other posts: 17194

average number of ask comments: 14.038417431192661
average number of show comments: 10.31669535283993

What actually happened:

The number of Ask Posts is: 9139
The number of Show Posts is: 10158
The number of Other Posts is: 273822

The total number of posts is: 293119

The average number of ask comments is 10.39
The average number of show comments is 4.89

1 Like

Hi @joasen,

I have copied your code and tried it both on my local computer and in the DQ platform. Everything works perfectly and coincides with the solution notebook:

opened_file = open('hacker_news.csv', encoding='utf8')
from csv import reader
read_file = reader(opened_file)
hn = list(read_file)
print(hn[:5])
print("\n")

header = hn[0]
hn_no_header = hn[1:] # Gets rid of the header for prepare data for analysis
print(hn_no_header[:5]) # Checks the header is not present in the first 5 rows of data

ask_posts = []
show_posts = []
other_posts = []

for row in hn_no_header:
    title = row[1]
    title_lower_case = title.lower() # converts title to all lowercase letters
    
    if title_lower_case.startswith('ask hn'):
        ask_posts.append(row)
        
    elif title_lower_case.startswith('show hn'):
            show_posts.append(row)
            
    else:
        other_posts.append(row)
        
ask_posts_length = len(ask_posts)
show_posts_length = len(show_posts)
other_posts_length = len(other_posts)

print("The number of Ask Posts is:",ask_posts_length)
print("\n")
print("The number of Show Posts is:",show_posts_length)
print("\n")
print("The number of Other Posts is:",other_posts_length)
print("\n")

print("The total number of posts is:", len(hn_no_header))
    
total_ask_comments = 0

for row in ask_posts:
    num_comments = int(row[4])
    total_ask_comments += num_comments
    
avg_ask_comments = total_ask_comments / len(ask_posts)
    
template = "The average number of ask comments is {num:.2f}" # :.2f to specify decimal to 2 d.p 
string = template.format(num=avg_ask_comments)
print(string)

total_show_comments = 0

for row in show_posts:
    num_comments = int(row[4])
    total_show_comments += num_comments
    
avg_show_comments = total_show_comments / len(show_posts)
    
template = "The average number of show comments is {num:.2f}" # :.2f to specify decimal to 2 d.p 
string = template.format(num=avg_show_comments)
print(string)

Output:

[['id', 'title', 'url', 'num_points', 'num_comments', 'author', 'created_at'], ['12224879', 'Interactive Dynamic Video', 'http://www.interactivedynamicvideo.com/', '386', '52', 'ne0phyte', '8/4/2016 11:52'], ['10975351', 'How to Use Open Source and Shut the ■■■■ Up at the Same Time', 'http://hueniverse.com/2016/01/26/how-to-use-open-source-and-shut-the-■■■■-up-at-the-same-time/', '39', '10', 'josep2', '1/26/2016 19:30'], ['11964716', "Florida DJs May Face Felony for April Fools' Water Joke", 'http://www.thewire.com/entertainment/2013/04/florida-djs-april-fools-water-joke/63798/', '2', '1', 'vezycash', '6/23/2016 22:20'], ['11919867', 'Technology ventures: From Idea to Enterprise', 'https://www.amazon.com/Technology-Ventures-Enterprise-Thomas-Byers/dp/0073523429', '3', '1', 'hswarna', '6/17/2016 0:01']]


[['12224879', 'Interactive Dynamic Video', 'http://www.interactivedynamicvideo.com/', '386', '52', 'ne0phyte', '8/4/2016 11:52'], ['10975351', 'How to Use Open Source and Shut the ■■■■ Up at the Same Time', 'http://hueniverse.com/2016/01/26/how-to-use-open-source-and-shut-the-■■■■-up-at-the-same-time/', '39', '10', 'josep2', '1/26/2016 19:30'], ['11964716', "Florida DJs May Face Felony for April Fools' Water Joke", 'http://www.thewire.com/entertainment/2013/04/florida-djs-april-fools-water-joke/63798/', '2', '1', 'vezycash', '6/23/2016 22:20'], ['11919867', 'Technology ventures: From Idea to Enterprise', 'https://www.amazon.com/Technology-Ventures-Enterprise-Thomas-Byers/dp/0073523429', '3', '1', 'hswarna', '6/17/2016 0:01'], ['10301696', 'Note by Note: The Making of Steinway L1037 (2007)', 'http://www.nytimes.com/2007/11/07/movies/07stein.html?_r=0', '8', '2', 'walterbell', '9/30/2015 4:12']]
The number of Ask Posts is: 1744


The number of Show Posts is: 1162


The number of Other Posts is: 17194


The total number of posts is: 20100
The average number of ask comments is 14.04
The average number of show comments is 10.32

Well, it’s practically your copied code, I only inserted print("\n") between your first 2 print statements.

1 Like