0
0
Prompt Engineering / GenAIml~10 mins

Why API access enables integration in Prompt Engineering / GenAI - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to call an API endpoint using Python's requests library.

Prompt Engineering / GenAI
import requests
response = requests.[1]('https://api.example.com/data')
print(response.status_code)
Drag options to blanks, or click blank then click option'
Aget
Bpost
Cput
Ddelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'post' instead of 'get' when only fetching data.
2fill in blank
medium

Complete the code to extract JSON data from the API response.

Prompt Engineering / GenAI
import requests
response = requests.get('https://api.example.com/data')
data = response.[1]()
print(data)
Drag options to blanks, or click blank then click option'
Atext
Bcontent
Cjson
Draw
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'text' returns raw string, not parsed JSON.
3fill in blank
hard

Fix the error in the code to send a POST request with JSON data.

Prompt Engineering / GenAI
import requests
payload = {'name': 'Alice', 'age': 30}
response = requests.post('https://api.example.com/users', [1]=payload)
print(response.status_code)
Drag options to blanks, or click blank then click option'
Adata
Bjson
Cparams
Dheaders
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'data' sends form data, not JSON, causing API errors.
4fill in blank
hard

Fill both blanks to check if the API response was successful and print the JSON content.

Prompt Engineering / GenAI
import requests
response = requests.get('https://api.example.com/data')
if response.[1] == [2]:
    print(response.json())
Drag options to blanks, or click blank then click option'
Astatus_code
B200
Cok
Djson
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ok' which is a boolean, not a numeric code.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that filters API data for items with 'active' status.

Prompt Engineering / GenAI
data = [{'id': 1, 'status': 'active'}, {'id': 2, 'status': 'inactive'}, {'id': 3, 'status': 'active'}]
active_items = {item['id']: item for item in data if item[1]'status'] [2] [3]
print(active_items)
Drag options to blanks, or click blank then click option'
A[
B==
C'active'
D['
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of brackets for dictionary keys.