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 Map and Filter Comprehension

Erik Burmeister
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Erik Burmeister
Python Web Development Techdegree Graduate 17,108 Points

Map and Filter Comprehension Challenge - Solve with range()?

Hey, Y'all, my question is the following: is it possible to solve this question using range()?

Here's the question to the problem: As an American, I don't understand Celsius temperatures. Using c_to_f and a list comprehension, create a variable named good_temps. Convert each Celsius temperature into Fahrenheit, but only if the Celsius temperature is between 9 and 32.6.

temperatures = [37,0,25,100,13.2,29.9,18.6,32.8]

def c_to_f(temp):
    """Returns Celsius temperature as Fahrenheit"""
    return temp * (9/5) + 32 

I tried using:

good_temps = [c_to_f(temp) for temp in temperatures if temp in range(9, 32.7)]

It doesn't pass. A solution I found is:

good_temps = [c_to_f(temp) for temp in temperatures if temp > 9 and temp < 32.6]

Could anyone explain why using range() doesn't work? I'm under the impression that they both achieve the same thing. Please, tell me if I'm wrong and why. If it's not too much trouble.

Thanks!

1 Answer

Steven Parker
Steven Parker
231,269 Points

For one thing, "range" only takes integer arguments. But even if you substitute 33 for 32.7, it creates a list of integers that span the two values. So the values to be converted that are not integers would not be found in the range, and so those values would get skipped over.