18. Lambda Expressions


#1

garbled = "IXXX aXXmX aXXXnXoXXXXXtXhXeXXXXrX sXXXXeXcXXXrXeXt mXXeXsXXXsXaXXXXXXgXeX!XX"

def remove_garbage(garbled) :
message = ""
for char in garbled :
if char != 'X' :
message += char
return message
message = remove_garbage(garbled)
print message

this is my code but it says this
File "python", line 8
SyntaxError: 'return' outside function


#2

Is there a question here?
You have an error message there, what is it telling you? What do you think it means, what have you tried to do about it?
Make sure that the code in your post is properly formatted so that others are able to test/run the code.


#3

hey you need not not to write such a long code
check this out
garbled = "IXXX aXXmX aXXXnXoXXXXXtXhXeXXXXrX sXXXXeXcXXXrXeXt mXXeXsXXXsXaXXXXXXgXeX!XX"
message = filter(lambda x: x != "X", garbled)
print message


#4

garbled = "IXXX aXXmX aXXXnXoXXXXXtXhXeXXXXrX sXXXXeXcXXXrXeXt mXXeXsXXXsXaXXXXXXgXeX!XX"

I thought this way:

# for resposta in garbled:
#     if resposta != 'X':
#         print resposta,
#         I   a m   a n o t h e r   s e c r e t   m e s s a g e !

And I did so lambda.

message = filter(lambda text: [resposta for resposta in text if resposta != 'X'], garbled)
print message


#6

They are wanting you to use lambda expressions instead of defining a function. This is my code maybe you can work backwards from it to see how I was thinking.

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

But to fix your function:

garbled = "IXXX aXindent preformatted text by 4 spacesXmX aXXXnXoXXXXXtXhXeXXXXrX sXXXXeXcXXXrXeXt mXXeXsXXXsXaXXXXXXgXeX!XX"

def remove_garbage(garbled):
    message = ""
    for char in garbled:
        if char != 'X':
            message += char
    print message
    return message
remove_garbage(garbled)

Your syntax was probably a bit off, your "return message" needed to be in line with your "for" loop. I also found that if you want to print to the console it's better to print before returning or call for print when you call your function.

Hope this helped you mate and good luck!


#7

Do it this way:

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

#8

hi what does x! mean?

is there a meaning for the sign !


#9

The letter 'x' means the element of the variable 'grabled', which in this context, its letters. When we iterate thru 'x' in 'grabled', if 'x' is not equal to 'X' OR x != "X" ( != is 'not equal to'), then 'x' will be printed.


#10

You are asked to use a lambda function as a filter. What passes is what is not "X" -> x != "X"

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


#12

Yep!

Only difference I had was I used 'y' in the lanbda instead of 'x'


#13

There's no need for that code to be that long. This is my option:

message = filter(lambda x: x is not 'X', garbled)
print message


#15

This is my code and it works :slight_smile:

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


#16

for loop must be aligned below the message ="",you know Python's indentation rules,
so make changes from for loop on words
you can see my code here.so you can see the difference .hope it helpful

garbled = "IXXX aXXmX aXXXnXoXXXXXtXhXeXXXXrX sXXXXeXcXXXrXeXt mXXeXsXXXsXaXXXXXXgXeX!XX"

def remove_garbage(garbled) :
    message = ""
    for char in garbled :
        if char != 'X' :
            message += char
    return message
message = remove_garbage(garbled)
print message

#17