Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Product Revenues Capstone | Functions
Introduction to Python
Section 6. Chapter 7
single

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.

Task

Swipe to start coding

Follow these step-by-step instructions to complete the task:

  • Initialize a list named products that contains the product names;

  • Initialize a list named prices that contains the price of each product;

  • Initialize a list named quantities_sold that 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 revenue list from the function;

  • Use the zip() function to combine the products list and the revenue list into a list of tuples named revenue_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 revenues list 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

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 6. Chapter 7
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

some-alt