31 Years of Python | 48 Hour Sale Extension!!!
days
hours
minutes
seconds

Hacker News Project - Cannot convert to integer

Hello! In line 3, I am converting the number of comments given in strings to integer, however I am getting an error although my code is similar to those from the solution. How can I address this issue?

total_ask_comments = 0

for post in ask_posts:
n_comments = int(post[4])
total_ask_comments += n_comments

avg_ask_comments = total_ask_comments / len(ask_posts)
print(avg_ask_comments)


ValueError Traceback (most recent call last)
in
2
3 for post in ask_posts:
----> 4 n_comments = int(post[4])
5 total_ask_comments += n_comments
6

ValueError: invalid literal for int() with base 10: β€˜H’

Uploading the photo if the above is not clear!

1 Like

Hi @adilbek.aizhan09

Please review this guideline for future queries as it’s sometimes difficult for other learners to help you with non-formatted code. Please also attach the screen link of the lesson/task/ project page that you are facing difficulties with.

Guidelines for asking a technical question in our Community

try to print the results of just this code post[4] inside your for loop. Observe the results, are all values numerical or alphanumeric as well? In the case of the latter (which is the case based on the error) try to print all the elements of the post list. Perhaps the target value is at a different index?!

Please share the results for both prints post[4] and post.

1 Like