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

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)


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 HN: How to improve my personal website?',
  '8/16/2016 9:55'],
  'Ask HN: Am I the only one outraged by Twitter shutting down share counts?',
  '11/22/2015 13:43'],
  'Ask HN: Aby recent changes to CSS that broke mobile?',
  '5/2/2016 10:14'],
  'Ask HN: Looking for Employee #3 How do I do it?',
  '8/2/2016 14:20'],
  'Ask HN: Someone offered to buy my browser extension from me. What now?',
  '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!!