18. Lambda Expressions


#1

I am stuck on the last exercise, here.

My code is:

garbled = "IXXX aXXmX aXXXnXoXXXXXtXhXeXXXXrX sXXXXeXcXXXrXeXt mXXeXsXXXsXaXXXXXXgXeX!XX"

def remove_garbage(garbled) :
    message = filter(lambda x: x != "X", garbled)
for char in garbled :
    if char != 'X' :
        message += char
    return message

print remove_garbage(garbled)

My error message is:

Oops, try again.
    Your syntax doesn't look quite right. Take a look at the Hint if you need help!

  File "python", line 8
SyntaxError: 'return' outside function

How do I get 'return' inside the function?


#2

Simplicity is key you can do this in only three lines and I will explain how,

#Garbled text
garbled = "IXXX aXXmX aXXXnXoXXXXXtXhXeXXXXrX sXXXXeXcXXXrXeXt mXXeXsXXXsXaXXXXXXgXeX!XX"

#Lamda function with filter method that removes all X occurences,

message = filter(lambda x: x != "X", garbled)

#Print out new message
print message

There is no need for all of this,


#3

Thank you! :slight_smile:


#4

Hi , would you tell me what is wrong with my code??

garbled = "IXXX aXXmX aXXXnXoXXXXXtXhXeXXXXrX sXXXXeXcXXXrXeXt mXXeXsXXXsXaXXXXXXgXeX!XX"
message = filter(lambda x: x != "x", garbled)
print message

#5

Make this a capital X and not lowercase,


#6

thanks.:heartbeat:.:blush:


#7

Thank you for your kind help! The main key here is actually the "X".


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.