Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 17. hn_submissions.py, KeyError #100

Closed
AlexHedgehog04 opened this issue Sep 24, 2020 · 3 comments
Closed

Chapter 17. hn_submissions.py, KeyError #100

AlexHedgehog04 opened this issue Sep 24, 2020 · 3 comments

Comments

@AlexHedgehog04
Copy link

Hello!
Working with the Hacker News API i've catched a keyerror in the first 'for' loop.

Traceback` (most recent call last):
  File "...\working_with_apis\hn_submissions.py", line 24, in <module>
    'comments': response_dict['descendants'],
KeyError: 'descendants'

But the key 'descendants' was actually in dictonary.
I checked every single literal (not literally, but nearby :) ). The code was exactly like in the book.
After time i've decided to try to decrease submission_ids from [:30] to [:10]. The error was magically disappeared. By increasing of the quantity submission_ids back to [:30] i found out, that one of thirty really dont have the key 'descendants'.
As i understand, its a problem of an API response, but it would be nice to add the way to handle the cases when some data in the API response is missing.

PS. So far i checked, nobody pushed that issue. Sorry, if someone did.

@ehmatthes
Copy link
Owner

Hi, there's an update for the second edition that addresses this: https://ehmatthes.github.io/pcc_2e/updates/sixth_printing/#chapter-17

@Windbell922
Copy link

I ran into the same issue while working on Chapter 17. Luckily, I came across your message; otherwise, I'd still be stuck. By the way, when I was using try-except-else, I referenced {submission_id} to pinpoint which wretched ID caused the error - it was 36924518! Hahahaha!

@shensuii
Copy link

shensuii commented Jun 7, 2024

Hello! Working with the Hacker News API i've catched a keyerror in the first 'for' loop.

Traceback` (most recent call last):
  File "...\working_with_apis\hn_submissions.py", line 24, in <module>
    'comments': response_dict['descendants'],
KeyError: 'descendants'

But the key 'descendants' was actually in dictonary. I checked every single literal (not literally, but nearby :) ). The code was exactly like in the book. After time i've decided to try to decrease submission_ids from [:30] to [:10]. The error was magically disappeared. By increasing of the quantity submission_ids back to [:30] i found out, that one of thirty really dont have the key 'descendants'. As i understand, its a problem of an API response, but it would be nice to add the way to handle the cases when some data in the API response is missing.

PS. So far i checked, nobody pushed that issue. Sorry, if someone did.

i encounter the same situation, thanks for your solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants