Python by Projects › Checkpoint 2 — Loops and Functions
Questions: Checkpoint 2 — Loops and Functions
You have 35–45 minutes for eight questions. Show your reasoning when a question asks you to trace or judge code. In a blank code cell, write sentence answers inside a complete print(...) line so Python can read them. Turtle code is reading-only here; no drawing window is needed.
Question 1
Trace this for loop. Predict the exact output, including the number of lines and the order of the integer loop-counter values.
for round_number in range(4):
print(round_number)
How this is checked: Checked by comparing your prediction with what the program really prints.
Answer format: several lines
Question 2
Complete the two blanks so the while loop adds the integers 1 through 4 to the accumulator and then stops. The incomplete code is shown as text. Rewrite the whole program in the blank cell, filling in both blanks so it runs.
count = 1
total = 0
while count <= 4:
total = __________
count = __________
print(total)
Explain why the comparison eventually becomes false.
How this is checked: You check this one yourself, against the list below.
The site cannot check this one. Run your program, then tick each item below when your work does it.
Question 3
Write a function named greeting that takes one parameter named name and returns an f-string greeting that includes that name.
Call the function with one sample name, store the returned value in message, and print message.
How this is checked: You check this one yourself, against the list below.
The site cannot check this one. Run your program, then tick each item below when your work does it.
Question 4
Read the two functions.
def show_bonus(points):
print(points + 2)
def get_bonus(points):
return points + 2
Which function hands a number back so the program can use it in more arithmetic? Which function only displays a number? Explain how return and print are different.
How this is checked: You check this one yourself, against the list below.
The site cannot check this one. Run your program, then tick each item below when your work does it.
Question 5
Trace the scopes in this program.
team = "Owls"
def make_tag(player):
tag = f"{team}: {player}"
print(tag)
make_tag("Dev")
print(team)
Predict the exact two output lines. Name the global variable and the local variable. Would print(tag) work after the function call? Explain why or why not.
How this is checked: Checked by comparing your prediction with what the program really prints.
Answer format: several lines
Question 6
Trace this full if/elif/else ladder. One condition is nested inside the elif branch and uses the logical operator or. Predict the one message that prints, then explain every comparison Python checks before reaching it.
energy = 7
has_key = True
knows_code = False
if energy >= 8:
print("Power door opens")
elif energy >= 5:
if has_key or knows_code:
print("Side door opens")
else:
print("Find a key")
else:
print("Recharge first")
How this is checked: Checked by comparing your prediction with what the program really prints.
Answer format: one line
Question 7
TURTLE TRACE/PREDICT ONLY — do not run this code. Read the loop and decide what closed shape the turtle draws. State the number of sides and explain how the repeated turn angles make one full 360-degree turn.
for i in range(5):
forward(100)
right(72)
How this is checked: You check this one yourself, against the list below.
The site cannot check this one. Run your program, then tick each item below when your work does it.
Question 8
Build a scoring function named score_round. It takes three Boolean parameters: first_correct, second_correct, and third_correct.
Inside the function, start an accumulator named score at 0. Use an if statement for each parameter to add 1 when it is true. Return the final integer score. Call the function with sample Boolean values and print the returned score.
How this is checked: You check this one yourself, against the list below.
The site cannot check this one. Run your program, then tick each item below when your work does it.