+ 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]))
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]))
+ 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
+ 3
here is my solution. hope it helps.
import re
text = input("")
text = re.sub("[^a-z A-Z]+", "",text)
print(str(text[::-1]))
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)
0
Akshaya Arunkumar put a space in the alphabet list
0
import re
x=input()
y=re.findall(r"[a-zA-Z\s]",x)
print("".join(reversed(y)))