Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.
Shade in the A or B circle for each question.
Draw a line connecting each item on the left to its match on the right.
| Left column | Right column | |
|---|---|---|
| 1response.status_code == 200 | AChecks whether the API call succeeded, because status code 200 means OK and the data came back. | |
| 2except requests.exceptions.RequestException: | Belif response.status_code == 404: | |
| 3A 404 status code means the resource was not found, so the program should tell the user rather than read the data. | Ctry: response = requests.get(url) | |
| 4Wrap requests.get() in a try block so that a connection failure can be handled by an except block. | DThe call is not in try/except and the status is not checked, so a failed call crashes the program or gives bad data. | |
| 5if response.status_code = 200: | ECatches a failed API call, such as no internet or a timeout, so the program can show a friendly message instead of crashing. | |
| 6response = requests.get(url) print(response.json()) | FA single = assigns a value, so it is a syntax error. Use == to compare the status code with 200. |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
response.status_code — Check a response's status code (200 OK, 404 not found) and wrap requests.get() in try/except so a failed API call shows a friendly message, not a crash.if response.status_code == 200:try:
response = requests.get(url)
except requests.exceptions.RequestException:response = requests.get(url)
if response.status_code == 404:
print("Not found")try:
r = requests.get(url, timeout=5)
except requests.exceptions.RequestException:except requests.exceptions.RequestException:
print("Could not connect.")