Receive, process and display external data

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Receive, process and display external data

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A prototype pipeline has three steps. Which order is correct?
A receive -> process -> display
B display -> receive -> process
2. Which function belongs to the RECEIVE step?
A def show_result(total): print(total)
B def get_data(url): return requests.get(url).json()
3. Which function belongs to the PROCESS step?
A def average(scores): return sum(scores) / len(scores)
B def load_file(): return open('data.csv').read()
4. Which function belongs to the DISPLAY step?
A def clean(rows): return [r.strip() for r in rows]
B def show(avg): print('Average:', avg)
5. Which main() follows the one-function-per-step design?
A data = receive() result = process(data) display(result)
B print(open('data.csv').read().upper())

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1data = receive_data() AThe receive step: a function gets external data (such as from a file or API) and stores it in a variable for later steps.
2show_result(summary) Bdef main(): data = receive_data() result = process_data(data) show_result(result)
3Each pipeline step gets its own function, and main() calls them in order: receive, process, then display. CThe steps are out of order. process_data(data) is called before data exists, which causes a NameError. The receive step must come first.
4The process step takes the received data as a parameter and returns a new value instead of printing it. Ddef process_data(data): return sum(data) / len(data)
5def main(): result = process_data(data) data = receive_data() show_result(result) EThe process function prints the result instead of returning it. Displaying is the job of the display step, and nothing is passed on to show_result.
6def process_data(data): total = sum(data) print(total) FThe display step: a function shows the processed result to the user, such as a printed summary.

Definitions

receive -> process -> display

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
def main(): data = ____________() result = process_data(data)
2.
def main(): ___ = receive_data() result = process_data(raw)
3.
def ______________(result): print("Average:", result)
4.
def average(values): total = sum(values) ______ total / len(values)
5.
response = requests.get(URL) raw = response.____() result = process_data(raw)

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A def receive()
B raw = '72,85,90'
C return raw
D data = receive()
Question 2
A def process(nums):
B total = sum(nums)
C return total * len(nums)
Question 3
A def display(avg):
B print('Results')
C print('-------')
D print('Average:' avg)
Question 4
A def main():
B raw = receive()
C avg = process(raw)
D display(raw)
E main()
Question 5
A import requests
B def receive(url):
C r = requests.post(url)
D return r.json()

Answer Key

Receive, process and display external data
Section A: Quick Questions
Q1
A — receive -> process -> display
Q2
B — def get_data(url): return requests.get(url).json()
Q3
A — def average(scores): return sum(scores) / len(scores)
Q4
B — def show(avg): print('Average:', avg)
Q5
A — data = receive() result = process(data) display(result)
Section B: Match the Pairs
Q1
data = receive_data() → The receive step: a function gets external data (such as from a file or API) and stores it in a variable for later steps.
Q2
show_result(summary) → The display step: a function shows the processed result to the user, such as a printed summary.
Q3
Each pipeline step gets its own function, and main() calls them in order: receive, process, then display. → def main(): data = receive_data() result = process_data(data) show_result(result)
Q4
The process step takes the received data as a parameter and returns a new value instead of printing it. → def process_data(data): return sum(data) / len(data)
Q5
def main(): result = process_data(data) data = receive_data() show_result(result) → The steps are out of order. process_data(data) is called before data exists, which causes a NameError. The receive step must come first.
Q6
def process_data(data): total = sum(data) print(total) → The process function prints the result instead of returning it. Displaying is the job of the display step, and nothing is passed on to show_result.
Section C: Definitions
Q1
receive -> process -> display — A prototype pipeline: get external data into a variable, process it with a function, then show the result. Each step has its own function, called from main().
Section D: Fill in the Blanks
Q1
def main(): data = receive_data() result = process_data(data)
Q2
def main(): raw = receive_data() result = process_data(raw)
Q3
def display_result(result): print("Average:", result)
Q4
def average(values): total = sum(values) return total / len(values)
Q5
response = requests.get(URL) raw = response.json() result = process_data(raw)
Section E: Spot the Error
Q1
A — A function definition must end with a colon. The first line should be def receive():
Q2
C — An average is the total divided by the count. The process step should return total / len(nums), not multiply.
Q3
D — print() arguments must be separated by a comma. It should be print('Average:', avg).
Q4
D — The display step should show the processed result. It must be display(avg), otherwise the process step is skipped.
Q5
C — The receive step reads data from an API, so it needs requests.get(url). post() is for sending data.