Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Functional Python The Lambda Lambada Reduce

This code i not passing

We have a bunch of prices and sales numbers and we need to find out our total earnings.

Let's start by writing a function named product_sales that takes a single two-member tuple made up of a price and a number of units sold. product_sales should return the product of the price and the number of units.

prices.py
prices = [
    (6.99, 5),
    (2.94, 15),
    (156.99, 2),
    (99.99, 4),
    (1.82, 102)
]

def product_sales(price, units):
    return price * units

2 Answers

Steven Parker
Steven Parker
221,451 Points

This function takes two individual arguments, but the instructions ask for a function " that takes a single two-member tuple"

So your declaration might look something like:

def product_sales(one_tuple):

And of course, the body would need to be adapted accordingly.

We have a bunch of prices and sales numbers and we need to find out our total earnings.

Let's start by writing a function named product_sales that takes a single two-member tuple made up of a price and a number of units sold. product_sales should return the product of the price and the number of units.

prices = [ (6.99, 5), (2.94, 15), (156.99, 2), (99.99, 4), (1.82, 102) ] def product_sale(single_two): price,units=single_two return price* units print(reduce(product_sale,prices))

NB why not working too

Steven Parker
Steven Parker
221,451 Points

Two issues here:

  • the instructions ask for a function named product_sales (plural) not "product_sale" (singular)
  • the "print" statement is not something asked for by the instructions and will confuse the evaluator
Learn QA
Learn QA
6,011 Points

def product_sales(one_tuple): product_sales = 21.0 return product_sales

this code passed but not sure how

Steven Parker
Steven Parker
221,451 Points

Looks like you tricked the challenge by guessing the result for the values it tested with.
But a valid solution would use the argument and calculate) the result.