Challenge: Product Revenues Capstone
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 per item for each product; - Initialize a list named
quantities_soldthat contains the number of items sold for each product; - Calculate the revenue for each product by multiplying the price by the quantity sold, and store all results in a new list called
revenue; - Use the
zip()function to combine theproductsandrevenuelists into a list of tuples namedrevenue_per_product, where each tuple contains a product name and its corresponding revenue; - Sort the
revenue_per_productlist alphabetically by product name; - Print each product and its revenue using this format:
<product_name> has total revenue of $<revenue>.
You must define the following functions:
calculate_revenue(prices, quantities_sold): This function should multiply each price by its corresponding quantity sold, store the results in a list, and return this list of revenues.formatted_output(revenues): This function should take a list of(product_name, revenue)tuples, sort them alphabetically by product name, and print each in the specified format.
After defining these functions, use the provided lists to call them and display the results as described above.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 2.17
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 per item for each product; - Initialize a list named
quantities_soldthat contains the number of items sold for each product; - Calculate the revenue for each product by multiplying the price by the quantity sold, and store all results in a new list called
revenue; - Use the
zip()function to combine theproductsandrevenuelists into a list of tuples namedrevenue_per_product, where each tuple contains a product name and its corresponding revenue; - Sort the
revenue_per_productlist alphabetically by product name; - Print each product and its revenue using this format:
<product_name> has total revenue of $<revenue>.
You must define the following functions:
calculate_revenue(prices, quantities_sold): This function should multiply each price by its corresponding quantity sold, store the results in a list, and return this list of revenues.formatted_output(revenues): This function should take a list of(product_name, revenue)tuples, sort them alphabetically by product name, and print each in the specified format.
After defining these functions, use the provided lists to call them and display the results as described above.
Solution
Thanks for your feedback!
single