Note: Click on "Kernel" > "Restart Kernel and Run All" in JupyterLab after finishing the exercises to ensure that your solution runs top to bottom without any errors. If you cannot run this file on your machine, you may want to open it in the cloud .
The exercises below assume that you have read Chapter 3 .
The ...
's in the code cells indicate where you need to fill in code snippets. The number of ...
's within a code cell give you a rough idea of how many lines of code are needed to solve the task. You should not need to create any additional code cells for your final solution. However, you may want to use temporary code cells to try out some ideas.
Q1: Write a function discounted_price()
that takes the positional arguments unit_price
(of type float
) and quantity
(of type int
) and implements a discount scheme for a line item in a customer order as follows:
Only one of the two discounts is granted, whichever is better for the customer.
The function should then return the overall price for the line item. Do not forget to round appropriately.
def discounted_price(unit_price, quantity):
"""Calculate the price of a line item in an order.
Args:
unit_price (float): price of one ordered item
quantity (int): number of items ordered
Returns:
line_item_price (float)
"""
...
...
...
...
...
...
...
...
...
return ...
Q2: Calculate the final price for the following line items of an order:
discounted_price(...)
discounted_price(...)
discounted_price(...)
discounted_price(...)
Q3: Calculate the last two line items with order quantities of 20 and 15. What do you observe?
discounted_price(...)
discounted_price(...)
Q4: Looking at the if
-else
-logic in the function, why do you think the four example line items in Q2 were chosen as they were?
< your answer >