GET, status, JSON — the call AI makes 100 times a day — step 9 of 9
One last thing before we move on. Same surface as a write step — but the lesson doesn't complete until this passes.
Final drill. Below is a function fetch that simulates an API call —
it returns a (status_code, body) tuple. Write a function safe_fetch
that:
- Calls
fetch(user_id). - If the status is
200, returnsbody["name"]. - For any other status, returns the string
"error".
Then the script will call safe_fetch(7) and safe_fetch(404) and
print both. Expected output:
maya
error
One last thing before we move on. Same surface as a write step — but the lesson doesn't complete until this passes.
Final drill. Below is a function fetch that simulates an API call —
it returns a (status_code, body) tuple. Write a function safe_fetch
that:
- Calls
fetch(user_id). - If the status is
200, returnsbody["name"]. - For any other status, returns the string
"error".
Then the script will call safe_fetch(7) and safe_fetch(404) and
print both. Expected output:
maya
error
this step needs the editor
on desktop today; in the app (coming soon). save your spot and we'll bring you back here when you're ready.