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 (generated task tests)

Swipe to show menu

book
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.

Task

Swipe to start coding

Calculate and display product revenues using functions, with results presented in a clear, formatted way.

  • Define calculate_revenue(prices, quantities_sold):

    • Multiply each pair of elements from prices and quantities_sold;
    • Store the results in a list called revenue and return it.
  • Define formatted_output(revenues):

    • Accepts a list of tuples: (product_name, revenue);
    • Sort the list alphabetically by product name;
    • Print each product and its revenue using the specified format.
  • Use calculate_revenue() to generate the revenue list.

  • Use zip() to combine product_names and revenue into a list of tuples called revenue_per_product.

  • Call formatted_output() to print the sorted results.

Output Requirements

  • For each product, print:
    <product_name> has total revenue of $<revenue>

  • Ensure products are sorted alphabetically before printing.

Requirements checklist

  1. Call calculate_revenue([0.50, 1.20, 2.50, 2.00], [150, 200, 100, 50]) and check that the returned list has the same length as the input lists and each element is equal to the product of the corresponding elements from the input lists.
  2. Call formatted_output([("Bread", 75.0), ("Apples", 240.0), ("Oranges", 250.0), ("Bananas", 100.0)]) and check that the printed output contains four lines, each matching the format <product_name> has total revenue of $<revenue>, with the product names in alphabetical order.
  3. Check that the output for the provided example data contains the lines: "Apples has total revenue of $240.0", "Bananas has total revenue of $100.0", "Bread has total revenue of $75.0", and "Oranges has total revenue of $250.0".
  4. Check that the function formatted_output sorts the input list of tuples by product name alphabetically before printing, regardless of the input order.
  5. Check that the function calculate_revenue does not modify the input lists.
  6. Check that the function formatted_output does not return any value (returns None).

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

close

Awesome!

Completion rate improved to 2.17

book
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.

Task

Swipe to start coding

Calculate and display product revenues using functions, with results presented in a clear, formatted way.

  • Define calculate_revenue(prices, quantities_sold):

    • Multiply each pair of elements from prices and quantities_sold;
    • Store the results in a list called revenue and return it.
  • Define formatted_output(revenues):

    • Accepts a list of tuples: (product_name, revenue);
    • Sort the list alphabetically by product name;
    • Print each product and its revenue using the specified format.
  • Use calculate_revenue() to generate the revenue list.

  • Use zip() to combine product_names and revenue into a list of tuples called revenue_per_product.

  • Call formatted_output() to print the sorted results.

Output Requirements

  • For each product, print:
    <product_name> has total revenue of $<revenue>

  • Ensure products are sorted alphabetically before printing.

Requirements checklist

  1. Call calculate_revenue([0.50, 1.20, 2.50, 2.00], [150, 200, 100, 50]) and check that the returned list has the same length as the input lists and each element is equal to the product of the corresponding elements from the input lists.
  2. Call formatted_output([("Bread", 75.0), ("Apples", 240.0), ("Oranges", 250.0), ("Bananas", 100.0)]) and check that the printed output contains four lines, each matching the format <product_name> has total revenue of $<revenue>, with the product names in alphabetical order.
  3. Check that the output for the provided example data contains the lines: "Apples has total revenue of $240.0", "Bananas has total revenue of $100.0", "Bread has total revenue of $75.0", and "Oranges has total revenue of $250.0".
  4. Check that the function formatted_output sorts the input list of tuples by product name alphabetically before printing, regardless of the input order.
  5. Check that the function calculate_revenue does not modify the input lists.
  6. Check that the function formatted_output does not return any value (returns None).

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!

close

Awesome!

Completion rate improved to 2.17

Swipe to show menu

some-alt