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 Functional Workhorses Chaining Functions

AJ Salmon
AJ Salmon
5,675 Points

Code seems the same to me

While following along with the chaining functions video, I get this attribute error for this code:

def is_good_deal(book):
    return book.price <= 5

cheap_books = sorted(
    filter(is_good_deal, map(sales_price, BOOKS)),
    key=attrgetter('price')
)

print(cheap_books[0])
treehouse:~/workspace$ python stock.py                                                                                                                                 
Traceback (most recent call last):                                                                                                                                     
  File "stock.py", line 84, in <module>                                                                                                                                
    key=attrgetter('price')                                                                                                                                            
  File "stock.py", line 80, in is_good_deal                                                                                                                            
    return book.price <= 5                                                                                                                                             
AttributeError: 'set' object has no attribute 'price' 

I assume the rest of my files are the same as his, because they were written by him, haha. If you can shed any light on what I'm doing wrong, or need to see the .json file or the rest of the code, I'd appreciate some input! Thanks,

AJ

1 Answer

Steven Parker
Steven Parker
231,275 Points

This particular code does look OK.

But the error message is suggesting there may be something wrong with the "sales_price" function, or perhaps with the "BOOKS" data.

You can easily share the entire project if you make a snapshot of your workspace and post the link to it here.

AJ Salmon
AJ Salmon
5,675 Points

Ah, thank you, Steven! I was hoping my mistake was in the code I had written that day, to save someone the trouble of going through all of it. Here's the snapshot, and again, It's not a huge deal, but I really appreciate the troubleshooting help.

Steven Parker
Steven Parker
231,275 Points

Looks like I guessed right about the "sales_price" function. On line 52, it has "return {book.title, book.price}" which creates and returns a 2-item set object. But it should return the book object instead: "return book".

Also, while this has no functional impact, I noticed a stray extra apostrophe at the beginning of the title "Salem's Lot" on line 168 of books.json.

AJ Salmon
AJ Salmon
5,675 Points

Thanks a ton man, you da best