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

bookChallenge: 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.

Task

Swipe to start coding

Follow these steps to complete the task:

  • You will be given three lists: one with product names, one with prices, and one with quantities sold.
  • Calculate the revenue for each product by multiplying its price by the quantity sold.
  • Store the calculated revenue values in a new list.
  • Use the zip() function to pair each product name with its revenue, creating a list of tuples.
  • Sort this list of tuples alphabetically by product name.
  • Print each product and its revenue using this format: <product_name> has total revenue of $<revenue>.

Define the following functions:

  • calculate_revenue(prices, quantities_sold): This function should multiply each price by its corresponding quantity, store each result 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

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

close

Awesome!

Completion rate improved to 2.17

bookChallenge: 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 complete the task:

  • You will be given three lists: one with product names, one with prices, and one with quantities sold.
  • Calculate the revenue for each product by multiplying its price by the quantity sold.
  • Store the calculated revenue values in a new list.
  • Use the zip() function to pair each product name with its revenue, creating a list of tuples.
  • Sort this list of tuples alphabetically by product name.
  • Print each product and its revenue using this format: <product_name> has total revenue of $<revenue>.

Define the following functions:

  • calculate_revenue(prices, quantities_sold): This function should multiply each price by its corresponding quantity, store each result 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

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

some-alt