ValueError: invalid literal for int() with base 10: ‘h’

Hello Nguyen, I am having similar problem. How did you sort it out.

total_ask_comments = 0
for row in ask_posts:

total_ask_comments +=  int(row[4])

avg_ask_comment = total_ask_comments / len(ask_posts)

ValueErrorTraceback (most recent call last)
in ()
2 for row in ask_posts:
----> 4 total_ask_comments += int(row[4])
6 avg_ask_comment = total_ask_comments / len(ask_posts)

ValueError: invalid literal for int() with base 10: ‘h’

1 Like

Hi @khalidhann,

Welcome to the community! We would be happy to help you with it. To help you better, I would recommend you to upload your notebook (.ipynb) file.


Hi @khalidhann, here is the solution, use this link

1 Like

Computers store numbers in a variety of different ways. Python has two main ones. Integers, which store whole numbers (ℤ), and floating point numbers, which store real numbers (ℝ). You need to use the right one based on what you require. This error message invalid literal for int() with base 10 would seem to indicate that you are passing a string that’s not an integer to the int() function . In other words it’s either empty, or has a character in it other than a digit.

You can solve this error by using Python isdigit() method to check whether the value is number or not. The returns True if all the characters are digits, otherwise False .

val = "10.10"
if val.isdigit():

The other way to overcome this issue is to wrap your code inside a Python try…except block to handle this error.

Or if you are trying to convert a float string (eg. “10.10”) to an integer, simply calling float first then converting that to an int will work:

output = int(float(input))

HI @khalidhann ,

Great question! Did you append the “row” to ask_posts in the previous step? Currently the value at row[4] is not converting to int. Try to print the value to make sure it is a number. You can add a print statement right under total_ask_comments += int(row[4])

In the previous step, did you append the row:
ask_posts =

for row in hn:
title = row[1]
title = title.lower()
if title.startswith(“ask hn”):