Going fast! #DataquestChallenge Premium Annual Offer:
500 get 50% & the next 1000 get 40% off.
GET OFFER CODE

Stuck on error: ValueError: could not convert string to float: 'h'

Screen Link: Learn data science with Python and R projects

My Code:

total_ask_comments = 0

for row in ask_posts:
    ask_comments = int(float(row[4]))
    total_ask_comments += ask_comment

avg_ask_comments = total_ask_comments/len(ask_posts)

print(avg_ask_comments)

What I expected to happen: To be able to convert to a float.

What actually happened:

ValueError: could not convert string to float: 'h'

I suspect there are some comment values that cannot be converted to a float, how do I solve this?

I think you are correct here; the problem is coming from not being able to convert some values of row[4] to an integer. Since row[4] comes from your ask_posts variable, I would inspect this variable and see what values appear at index 4. Here is what I get on my end when I inspect this variable:

ask_posts[:5]

[['12296411',
  'Ask HN: How to improve my personal website?',
  '',
  '2',
  '6',
  'ahmedbaracat',
  '8/16/2016 9:55'],
 ['10610020',
  'Ask HN: Am I the only one outraged by Twitter shutting down share counts?',
  '',
  '28',
  '29',
  'tkfx',
  '11/22/2015 13:43'],
 ['11610310',
  'Ask HN: Aby recent changes to CSS that broke mobile?',
  '',
  '1',
  '1',
  'polskibus',
  '5/2/2016 10:14'],
 ['12210105',
  'Ask HN: Looking for Employee #3 How do I do it?',
  '',
  '1',
  '3',
  'sph130',
  '8/2/2016 14:20'],
 ['10394168',
  'Ask HN: Someone offered to buy my browser extension from me. What now?',
  '',
  '28',
  '17',
  'roykolak',
  '10/15/2015 16:38']]

I suspect you will see something quite different on your end which means you need to figure out why we have such different versions of ask_posts.

A couple other things I noticed in your code were in this loop:

You are using two different variables inside your for loop here: ask_comments and ask_comment. I believe you only want to be using one so you may want to add/remove an s to that they are the same.

Also, we don’t need to cast the number of comments to a float and then to an int…we can just go straight to an int.

Hope this helps and let me know if I can be of any further assistance!

Thank you so much! This really helps!!