Why isn't working?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why isn't working??

n = input() c = 0 for i in n: if i in "qwertyuioplkjhgfdsazxcvbnmMNBVCXZASDFGHJKLPOIUYTREWQRTYU": print(i[::-1]) Here is the question: You are a secret agent, and you receive an encrypted message that needs to be decoded. The code that is being used flips the message backwards and inserts non-alphabetic characters in the message to make it hard to decipher. 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. Sample Input: d89%l++5r19o7W *o=l645le9H Sample Output: Hello World

2nd Jul 2021, 4:26 PM
Shahir
Shahir - avatar
3 Answers
+ 4
Your code also removes the gap.You should also include gap " " in your code. Also you have to write print(i[::-1],end="") instead of print(i[::-1]). Maybe something like this: https://code.sololearn.com/ca99a16a6A12
2nd Jul 2021, 4:51 PM
The future is now thanks to science
The future is now thanks to science - avatar
+ 4
1. Why have c = 0? You don't use it elsewhere in the code. You may have been planning for c to be the reversed list of characters. 2. I would use the built-in string method isalpha() in place of the letters, e.g. if i.isalpha() or i = " ". 3. print(i[0:0:-1]) only prints the character, not the reversed string. 4. At some point, you will have to rejoin the characters using the built-in string method join() or using print() parameters. 5. If you want to iterate through the string backwards, you can say for i in n[0:0:-1]: 6. You can build the desired list of characters using append() 7. Much simpler and more 'Pythonic' would be to use a comprehension like a = (b for b in c if <some expression involving b>)
2nd Jul 2021, 5:10 PM
David Ashton
David Ashton - avatar
+ 2
I have corrected your code See at the code if you have questions , please ask. https://code.sololearn.com/cBQHNnDX2pb3/?ref=app
2nd Jul 2021, 5:00 PM
Angela
Angela - avatar