17. List Slicing-Printing the message


#1

My code works correctly but why won't the hidden message print to the console? I can print the contents of garbled until I reverse it and remove the x's, then it won't print the hidden message.


garbled = "!XeXgXaXsXsXeXmX XtXeXrXcXeXsX XeXhXtX XmXaX XI"
print garbled # Does print the contents of garbled
garbled= garbled[-1:0:2] # garbled reversed with x's removed
print garbled #Should print hidden message but doesn't 
message= garbled
print message


#2

How can you tell that it's correct if you're not able to confirm the result?
It's printed just fine from what I can tell. There just isn't very much to print, you're creating an empty string!


#3

When I submit the lesson, codecademy indicates that it is the correct answer and prompts me to the next lesson. Why would it print an empty string? It should print "I am the secret message!" The question is, since it prints the variable garbled, why won't it print the variable garbled or the variable message once it has been ungarbled? I ran it in Python's idle and it doesn't print there either. It only prints the original content of garbled.


#4

Because that's the value that your variable refers to.

codecademy indicates that it is the correct answer

Then it's either wrong or you have different ideas of what's correct. If you haven't seen the result yourself then you've got no way of telling that your code does what you think it does, which is all that matters.


#5

Hey OP,

My code is a lot more flat than yours but what I don't understand why you assigned
garbled = garbled[-1:0:2] only to later do this:

message = garbled

Why not just have assigned message to garbled[-1:0:2]intially?

`
Continuing the discussion from 17. List Slicing-Printing the message:


#6

Thanks for the help. I corrected the error. I was confused because Codecademy was telling me the code worked but it wouldn't print the result.


#7

the correct answer is:
message = garbled[::-2]
print message


#8

OK fine ! BUT:

Could anyone explain why this doesn't work:

garbled = "!XeXgXaXsXsXeXmX XtXeXrXcXeXsX XeXhXtX XmXaX XI"

# apparently the start/end/stride cannot be expressed like this: why ?

message = garbled[2:len(garbled):-2]
print message

while this does:

garbled = "!XeXgXaXsXsXeXmX XtXeXrXcXeXsX XeXhXtX XmXaX XI"

message = garbled[::-2]
print message

so what?


#9

Mr. Hyrundo, if you use:
message = garbled[2:len(garbled): -2]
your new message will star in the first blank space not in the letter "I" as you expected.


#10

OUCH!

I didn't consider the string was reversed !

Thanks !


#11

Sorry for me it's very difficult to understand this place


#12

I found solution by difficult way but it could been passed due to validation. You can check it as the following;

garbled = "!XeXgXaXsXsXeXmX XtXeXrXcXeXsX XeXhXtX XmXaX XI"
backwards = garbled[::-1]
arr = backwards.split()
for y in range(len(arr)):
print filter(lambda message: message != 'X', arr[y]),


#13

I know this is a traditional way and a bit lengthy but the code works.

garbled = "!XeXgXaXsXsXeXmX XtXeXrXcXeXsX XeXhXtX XmXaX XI"
backwards = garbled[::-1]
print backwards
new = []
for x in backwards:
if x!= 'X':
new.append(x)
new = "".join(new)
print new


#14

Can somebody tell me why this doesn't work?

It prints out "I am the secret message", but I get an error when I save/submit saying "Oops, try again. Your message doesn't look quite right; instead of "I am the secret message!", it should be "!egassem terces eht ma I""

garbled = "!XeXgXaXsXsXeXmX XtXeXrXcXeXsX XeXhXtX XmXaX XI"

garbled = garbled[::-1]

message = garbled[::2]

print message

#15

That message means what?


#16

Did you get the solution for this?
I am facing the same problem.
The message is appearing correct on the console but it is not validating


#18

Yep , same problem to me

garbled = garbled[::-2]
message = garbled

it displays the correct message but gives this error:
Oops, try again. Your message doesn't look quite right; instead of "I am the secret message!", it should be "!gse ecsetm "

but if you write the way @arbol saying it works with no error.


#19

#20

This topic is old, if you have a question please make a new topic, thank you for your understanding