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 step-by-step instructions to complete the task:
-
Initialize a list named
productsthat contains the product names; -
Initialize a list named
pricesthat contains the price of each product; -
Initialize a list named
quantities_soldthat contains how many units of each product were sold; -
Define a function named
calculate_revenue(prices, quantities_sold); -
Inside this function, multiply each price by the matching quantity sold and store the results in a new list called
revenue; -
Return the
revenuelist from the function; -
Use the
zip()function to combine theproductslist and therevenuelist into a list of tuples namedrevenue_per_product; -
Each tuple should contain a product name and its corresponding total revenue;
-
Define a function named
formatted_output(revenues); -
Inside this function, sort the
revenueslist alphabetically by product name; -
Print each product and its revenue using this format:
<product_name> has total revenue of $<revenue>; -
Call
calculate_revenue(prices, quantities_sold)to calculate the revenues; -
Call
formatted_output(revenue_per_product)to display the final results.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat