+ 1
I don't know what is wrong with this code pls help
Spy life I'm python Task: Create a program that will take the encoded message, flip it around, remove any characters that are not a letter or a space, and output the hidden message. Input Format: A string of characters that represent the encoded message. Output Format: A string of character that represent the intended secret message. Code message= input('enter an encrypted message :') for x in message: if (x.isalpha() and x.isspace()): print(reversed(message))
9 Answers
+ 3
Pg1001 ,
there are some issues in the code. it could be done more simplified, but i have tried to be as close as possible to your code sample. see the comments:
message= input() # do not use any user prompt when doing a code coach exercise
fin_str = '' # a string to collect the valid characters
for x in message:
if (x.isalpha() or x.isspace()): # logical operator has to be *or*
fin_str = x + fin_str # collect the characters in reverse order
print(fin_str)
+ 6
Pg1001 ,
the code works as expected.
it looks like you are not able to copy and paste the code i mentioned. check that your final print(...) statement has the correct indentstion.
+ 4
message = input()
res=""#holds the filtered output
for char in message:
if (char.isalpha() or char == " "):
#does the checking whether it's either a character or space
res+=char
print(res[::-1]) # to flip it
+ 2
I am sure there is an easier way than I will explain:
If you are just extracting letters and space in reverse order:
Input: "tset 321 tset"
Output: "test 123 test"
I would traverse the input string backwards and append to a new string:
Input output
r w
abc^ 123 3
r w
abc^ 123 32
r w
abc^ 123 321
r w
abc^ 123 321
i
abc^ 123 321
r w
abc^ 123 321 c
etc....
+ 2
Lothar THANKS A LOT I GOT IT CORRECT THIS TIME đ
0
@lothar
the output is coming as this for your method
dl
dlr
dlro
dlroW
dlroW
dlroW o
dlroW ol
dlroW oll
dlroW olle
dlroW olleH
0
rathol@ thgil si tearg seod gip nital