Students can try following practice first:
final_price(price, rate=0.9) to return the discounted price, and save(price, rate=0.9) to return the saved amount.read_scores(n) to read n scores (list). Write stats(scores) to return min, max, average. Write drop_lowest(scores, k=1) to return a new list with the lowest k scores removed.Output example A:
Original price: 120
Discount rate (press Enter for default 0.9):
Final: $108.00, Saved: $12.00
Output example B:
How many scores? 5
Score #1: 90
Score #2: 75
Score #3: 88
Score #4: 95
Score #5: 70
Original: [90.0, 75.0, 88.0, 95.0, 70.0]
Min=70.0, Max=95.0, Avg=83.60
Drop how many lowest? 1
After drop: [90.0, 75.0, 88.0, 95.0]
Min=75.0, Max=95.0, Avg=87.00
Answer example A:
def final_price(price, rate=0.9):
"""Return discounted price. Default 10% off."""
return price * rate
def save(price, rate=0.9):
"""Return money saved by the discount."""
return price - final_price(price, rate)
p = float(input("Original price: "))
r = input("Discount rate (press Enter for default 0.9): ").strip()
rate = float(r) if r else 0.9
fp = final_price(p, rate)
sv = save(p, rate)
print(f"Final: ${fp:.2f}, Saved: ${sv:.2f}")
Answer example B:
def read_scores(n):
"""Read n scores into a list."""
lst = []
for i in range(n):
s = float(input(f"Score #{i+1}: "))
lst.append(s)
return lst
def stats(scores):
"""Return min, max, avg together (multiple returns)."""
return min(scores), max(scores), sum(scores) / len(scores)
def drop_lowest(scores, k=1):
"""Return a new list with k lowest scores removed."""
tmp = scores[:]
for _ in range(k):
if tmp:
low = min(tmp)
tmp.remove(low)
return tmp
n = int(input("How many scores? "))
scores = read_scores(n)
lo, hi, avg = stats(scores)
print("Original:", scores)
print(f"Min={lo}, Max={hi}, Avg={avg:.2f}")
k = int(input("Drop how many lowest? "))
kept = drop_lowest(scores, k)
lo2, hi2, avg2 = stats(kept)
print("After drop:", kept)
print(f"Min={lo2}, Max={hi2}, Avg={avg2:.2f}")
Through these hands-on practices, students learned how to use functions to organize program structure, encapsulate logic, and reuse code effectively. A function can take input parameters and use return to pass results back to the program, forming a complete “input–process–output” cycle. Mastering parameters and return values marks a key step from simple sequential coding toward structured programming.