single
Challenge: Product Revenues Capstone
Swipe to show menu
Let's put together everything you've learned in this section and apply it.
In this task, your objective is to calculate the total revenue for each product in a grocery store based on their prices and quantities sold.
After calculating the revenues, you will sort the products alphabetically and display the results in a formatted output.
Swipe to start coding
Follow these steps to calculate and display the revenue for each product:
-
Define a function named
calculate_revenue(prices, quantities_sold).Inside the function:
- Multiply each price by its corresponding quantity sold.
- Store the calculated values in a list named
revenue. - Return the
revenuelist.
-
Call
calculate_revenue()using thepricesandquantities_soldlists. Store the returned list in a variable namedrevenue. -
Use
zip()to combine theproductsandrevenuelists. -
Convert the zipped result into a list and store it in a variable named
revenue_per_product. -
Define a function named
formatted_output(revenues).Inside the function:
- Sort
revenuesalphabetically by product name. - Print each product and its revenue using the exact format below:
- Sort
<product_name> has total revenue of $<revenue>
- Call
formatted_output(revenue_per_product)to display the results.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat