Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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
231,269 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
231,269 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

def product_sales(one_tuple): product_sales = 21.0 return product_sales

this code passed but not sure how

Steven Parker
Steven Parker
231,269 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.