Python, Concept by Concept › Checkpoint 03: Functions & Randomness
Questions: Checkpoint 03: Functions & Randomness
Complete all seven questions. Define each requested function, return its result, and use the worked calls to check your work. Questions use only numbers and ordinary text labels; do not use collections. Do not use input().
Each question ends with a Real version note showing how the program can read its values with input().
Question 1: Prime Test
Specification: Define is_prime(n). Return False when n < 2. Otherwise start a local name divisor at 2 and use a while loop to test every divisor smaller than n. Return False if any divisor divides evenly; otherwise return True after the loop.
Worked sample calls:
is_prime(1) → False
is_prime(9) → False
is_prime(19) → True
Real program
The real program reads a number to test on one line with input().
How this is checked: Checked by tests that run your function is_prime.
Question 2: Parameterized Travel Cost
Specification: Define travel_cost(km_text, rate_text). Inside the function, convert the parameters with km = float(km_text) and rate = float(rate_text), then set the local name base = 3.5. Return round(base + km * rate, 2). Then call the function for "8" kilometres at a rate of "1.25". Save the result with cost = travel_cost("8", "1.25"), then print it with print(f"Trip cost: ${cost}").
Worked sample calls:
travel_cost("8", "1.25") → 13.5
travel_cost("15", "0.8") → 15.5
travel_cost("0", "2.5") → 3.5
Expected printed line:
Trip cost: $13.5
Real program
The real program reads a distance and a rate as text on separate lines with input().
How this is checked: Checked by tests that run your function travel_cost.
Question 3: Running-Total Function
Specification: Define sum_to_n(n). Start total at 0. Use for number in range(1, n + 1): and update the running total with total = total + number. Return total.
Worked sample calls:
sum_to_n(3) → 6
sum_to_n(7) → 28
sum_to_n(20) → 210
Real program
The real program reads a final number on one line with input().
How this is checked: Checked by tests that run your function sum_to_n.
Question 4: Count Primes
Specification: Keep your is_prime(n) function from Question 1. Define count_primes(n). Use for candidate in range(2, n + 1): and call is_prime(candidate). Add 1 to total only when that call returns True. Return the count.
Worked sample calls:
count_primes(5) → 3
count_primes(10) → 4
count_primes(20) → 8
Real program
The real program reads an upper bound on one line with input().
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: Award Ladder
Specification: Define award_level(points) with exactly one if/elif/elif/else ladder. Return "Gold" for at least 90 points, "Silver" for at least 70, "Bronze" for at least 50, and "Keep practicing" otherwise. Check thresholds from highest to lowest. Do not join text with +.
Worked sample calls:
award_level(93) → "Gold"
award_level(74) → "Silver"
award_level(58) → "Bronze"
award_level(30) → "Keep practicing"
Real program
The real program reads an award score on one line with input().
How this is checked: Checked by tests that run your function award_level.
Question 6: Number-Tool Score
Specification: Define balanced_score(a, b, c). Use max(max(a, b), c) for high, min(a, b, c) for low, abs(high - low) for spread, and sum(range(1, 4)) for bonus. Return round((a + b + c + bonus - spread) / 3, 1).
Worked sample calls:
balanced_score(8, 12, 10) → 10.7
balanced_score(20, 5, 11) → 9.0
balanced_score(7, 7, 7) → 9.0
Real program
The real program reads three numbers on separate lines with input().
How this is checked: Checked by tests that run your function balanced_score.
Question 7: Seeded High-Roll Count
Specification: Begin with import random using module style. Define count_high_rolls(trials). Inside the function, call random.seed(4) before the first random use so the answer is reproducible. Start total at 0. Roll with random.randint(1, 6) exactly trials times and add 1 when the roll is at least 5. Return total.
Worked sample calls:
count_high_rolls(5) → 1
count_high_rolls(12) → 2
count_high_rolls(30) → 5
Real program
The real program reads a number of trials on one line with input().
How this is checked: Checked by tests that run your function count_high_rolls.