Complete the code to catch an error when the API call fails.
try: response = api_call() except [1]: print("API call failed")
The Exception class catches all errors, which is useful for handling any API call failure.
Complete the code to check if the API response status code indicates rate limiting.
if response.status_code == [1]: print("Rate limit exceeded. Please wait.")
Status code 429 means too many requests, indicating rate limiting.
Fix the error in the code to retry the API call after a delay when rate limited.
import time if response.status_code == 429: time.sleep([1]) response = api_call()
The time.sleep() function requires a positive number of seconds as a float or int. Using 5 seconds is a common retry delay.
Fill both blanks to handle API errors and retry with exponential backoff.
max_retries = 3 retry_delay = 1 for attempt in range(max_retries): response = api_call() if response.status_code == 200: break elif response.status_code == [1]: time.sleep(retry_delay) retry_delay [2] 2
When rate limited (status 429), the code waits and doubles the delay each retry using *= for exponential backoff.
Fill all three blanks to log errors, handle rate limits, and raise exceptions after retries.
import logging max_retries = 2 retry_delay = 1 for i in range(max_retries + 1): response = api_call() if response.status_code == 200: break elif response.status_code == [1]: logging.warning(f"Rate limit hit, retrying in {retry_delay} seconds") time.sleep(retry_delay) retry_delay [2] 2 else: logging.error(f"API error: {response.status_code}") raise [3]("API call failed")
The code handles rate limit with status 429, doubles retry_delay with '*=', and raises a general Exception on other errors.