+ 3

The Spy Life

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. msg = input("") alphabet = list("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") def remove(x): for item in x: if item not in alphabet: x = x.replace(item, "") return x print remove(msg[::-1]) Why doesnt my code work? Thanks Edit: Solved. msg = input("") alphabet = list("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ") def func(x): for char in x: if char not in alphabet: x = x.replace(char, "") return x print (func(msg[::-1]))

20th Jan 2020, 4:13 AM
Toh De Kai
Toh De Kai - avatar
6 Antworten
+ 7
1. You forgot about the space, add a space to the end of your list. 2. return should be outside the loop, not inside. 3. print should have parenthesis: print(remove(msg[::-1]))
20th Jan 2020, 4:19 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 7
You can use regular expressions to match any non-string. And [::-1] to flip any string. For example import re inp = input() print(''.join(re.findall(r'[a-zA-Z]|\s', inp[::-1]))) "re.findall(r'[a-zA-Z]|\s', inp[::-1]" this returns a list of everything that matches a word (a-z or A-Z) in inp[::-1] which is the reverse of the string
23rd Jan 2020, 12:18 AM
Onuoha_ifeanyi
Onuoha_ifeanyi - avatar
+ 3
here is my solution. hope it helps. import re text = input("") text = re.sub("[^a-z A-Z]+", "",text) print(str(text[::-1]))
31st Oct 2020, 12:09 PM
Frederick Shanks
Frederick Shanks - avatar
0
The second testcase is not being passed My output:youaregreat Expected output:you are great This is my code string=input() list1=[] i=0 length=len(string) while(i<length): for letter in range(97,123): if(string[i]==chr(letter)): temp=list1.append(chr(letter)) else: letter+=1 i+=1 str="" str1=str.join(list1[::-1]) print(str1)
1st Jun 2020, 3:02 PM
Akshaya Arunkumar
0
Akshaya Arunkumar put a space in the alphabet list
8th Sep 2020, 3:51 AM
Eric Wang
Eric Wang - avatar
0
import re x=input() y=re.findall(r"[a-zA-Z\s]",x) print("".join(reversed(y)))
29th Dec 2021, 1:39 PM
Ayu Lestari Gunawan