Chapter 0 Β· Section 5 practice
Functions
Work from question 1 to 10. Write your code, click Run Code, review the result, then click Submit attempt. An attempt may be wrong: reference answers become available after you submit all 10. Each Python box is independent. Submission progress is saved in this browser.
1. Define and call
Warm-up. Define double(number) to return twice its input. Print double(4).
Write your answer, then run it.
2. Reuse a result
Warm-up. Define add_five(number), store add_five(10) in result, then print result and result * 2.
Write your answer, then run it.
3. Two parameters
Warm-up. Define rectangle_area(length, width). Print the areas for (5, 3) and (2.5, 4).
Write your answer, then run it.
4. Label the arguments
Build your skills. Define ticket_bill(price, quantity). Call it using named arguments quantity=3 and price=25, and print the result.
Write your answer, then run it.
5. Repair print versus return
Build your skills. The function should provide a number that can be stored and multiplied. Replace the display instruction inside the function with a return, then print result * 2.
Write your answer, then run it.
6. A fee as an input
Build your skills. Define net_sale(price, shares, fee_rate). Return price * shares minus that amount times fee_rate. Print the results at price 100, shares 10, fees 0 and 0.001.
Write your answer, then run it.
7. Convert inside a function
Build your skills. Define bill_from_text(price_text, quantity_text). Convert the inputs and return the numeric bill. Print its result for β12.50β and β4β with two decimal places.
Write your answer, then run it.
8. Choose a delivery charge
Challenge. Define delivery_fee(order_value): return 0 if the value is at least 100, otherwise return 15. Print the fees for 80, 100, and 120.
Write your answer, then run it.
9. Call a function in a loop
Challenge. Define passes(score) to return whether score is at least 60. Loop over the scores, printing each score and the returned Boolean.
Write your answer, then run it.
10. A discounted bill with delivery
Challenge. Define final_bill(price, quantity, discount_rate). Subtract the discount from the product cost, then add HK$15 delivery only if the discounted bill is below HK$100. Print bills for (40, 3, 0.1) and (25, 2, 0.1) with two decimals.
Write your answer, then run it.
Reference answers are locked until all 10 attempts are submitted.
1. Define and call
def double(number):
return number * 2
print(double(4))Expected output
8def defines the instructions; a call supplies the input and runs them.
2. Reuse a result
def add_five(number):
return number + 5
result = add_five(10)
print(result)
print(result * 2)Expected output
15
30return gives a number back to the caller; print alone would not return that number.
3. Two parameters
def rectangle_area(length, width):
return length * width
print(rectangle_area(5, 3))
print(rectangle_area(2.5, 4))Expected output
15
10.0Both inputs matter; a float input can produce a float area.
4. Label the arguments
def ticket_bill(price, quantity):
return price * quantity
print(ticket_bill(quantity=3, price=25))Expected output
75Named arguments identify the matching parameter even when written in a different order.
5. Repair print versus return
def triple(number):
return number * 3
result = triple(4)
print(result * 2)Expected output
24The original function displays a value and returns None. Returning the calculation lets the caller use 12.
6. A fee as an input
def net_sale(price, shares, fee_rate):
gross = price * shares
fee = gross * fee_rate
return gross - fee
print(net_sale(100, 10, 0))
print(net_sale(100, 10, 0.001))Expected output
1000
999.0The same function handles both assumptions. A 0.001 rate means a 0.1% commission.
7. Convert inside a function
def bill_from_text(price_text, quantity_text):
price = float(price_text)
quantity = int(quantity_text)
return price * quantity
bill = bill_from_text("12.50", "4")
print(f"HK${bill:.2f}")Expected output
HK$50.00Conversion and calculation happen inside the function; formatting happens at the call site.
8. Choose a delivery charge
def delivery_fee(order_value):
if order_value >= 100:
return 0
else:
return 15
print(delivery_fee(80))
print(delivery_fee(100))
print(delivery_fee(120))Expected output
15
0
0The equality boundary qualifies for free delivery under this classroom rule.
9. Call a function in a loop
def passes(score):
return score >= 60
scores = [55, 60, 85]
for score in scores:
print(score, passes(score))Expected output
55 False
60 True
85 TrueFunctions can return Boolean values, not only numbers.
10. A discounted bill with delivery
def final_bill(price, quantity, discount_rate):
subtotal = price * quantity
bill = subtotal * (1 - discount_rate)
if bill < 100:
bill = bill + 15
return bill
print(f"HK${final_bill(40, 3, 0.1):.2f}")
print(f"HK${final_bill(25, 2, 0.1):.2f}")Expected output
HK$108.00
HK$60.00The first discounted bill is 108 and gets no delivery fee; the second is 45 and gets a 15 fee.