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

Hello,

I'm a bit confused about what this question is asking - I’ve just added the three iterations of this code and I am not passing the challenge. Have I misinterpreted the question?

Here’s the code, with three solutions at the bottom

Many thanks, Dale

Challenge Task 1 of 1

As a dumb 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

def is_good_temp(temp_c):
  """Returns True if temp_c is between 9 and 32.6 degrees"""
  return (temp_c >= 9) and (temp_c <= 32.6)

# good_temps = filter(is_good_temp, map(c_to_f, temperatures)) 
# Bummer, did you use a list comprehension?

# good_temps = [temp_c for temp_c in filter(is_good_temp, map(c_to_f, temperatures))] # Bummer, did you use a list comprehension?

# good_temps = filter(is_good_temp, [temp_c for temp_c in map(c_to_f, temperatures)]) # Bummer, did you use a list comprehension?

good_temps = [x for x in map(c_to_f, temperatures) if is_good_temp(x) ] 
# Got some wrong values in good_temps

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

You don't need map to solve the first Task in this challenge:

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

Update: I was able to modify your code to pass the challenge. It issue is is_good_temp is comparing to target Celsius numbers, but the map has already converted the numbers to Fahrenheit. Here is a version of your code that passes. I didn't use your is_good_temp since it wasn't part of the challenge.

# Original
good_temps = [x for x in map(c_to_f, temperatures) if X > 9 and x < 32.6 ] 
# Passing version
good_temps = [x for x in list(map(c_to_f, temperatures)) if x > c_to_f(9) and x < c_to_f(32.6)]
Nathan Tallack
Nathan Tallack
22,164 Points

Wow. I was wracking my brain trying to see that. You've been doing this too long Chris. ;)

Thanks a lot Chris

"...but only if the Celsius temperature is between 9 and 32.6."

The question specifies the Celsius temperature, so why would you need to pass the '9' and '32.6' into the 'c_to_f' function? Because doesn't this convert the '9' and '32.6' into Fahrenheit values, so we're then checking against Fahrenheit values, not Celsius ones as specified in the question?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,468 Points

Tobias Edwards: Yes. The second solution is awkward. It is not a recommended solution, but rather shows a way the OP code might work. Since the OP solution maps all of the temperatures using c_to_f before limiting to the target range, the source list is already all Fahrenheit values. Therefore, to get the range check to work post-conversion, the range values must also be converted to Fahrenheit. The end result is the same, but this method is less efficient since it converts more values to Fahrenheit than necessary compared to limiting on the Celsius number range then converting to Fahrenheit.