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 steps to calculate and display the revenue for each product:

  1. 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 revenue list.
  2. Call calculate_revenue() using the prices and quantities_sold lists. Store the returned list in a variable named revenue.

  3. Use zip() to combine the products and revenue lists.

  4. Convert the zipped result into a list and store it in a variable named revenue_per_product.

  5. Define a function named formatted_output(revenues).

    Inside the function:

    • Sort revenues alphabetically by product name.
    • Print each product and its revenue using the exact format below:
<product_name> has total revenue of $<revenue>
  1. Call formatted_output(revenue_per_product) to display the 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