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.

Submitted: 0 / 10

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.