0
Secret Message
Could I solve It with re? https://code.sololearn.com/ciVha3KUphUX/?ref=app
5 ответов
+ 4
you need to handle all non alphabetic chars rather than only spaces:
for char in string:
if not char.isalpha():
encoded_string += char
else:
encoded_string += dict_encoder[char]
+ 2
shorter way would be to use dict.get() method with default value:
for char in string:
encoded_string += dict_encoder
.get(char,char)
then if char is not a key of dict, it will return the unchanged char ;)
+ 2
You can make your dict_encoder like this instead and replace everything between
alphabet = [......]
dict_encoder = dict(zip(alphabet, alphabet[::-1]))
string = input()
0
Simple and efficient code👇👇✌️ :
import string
inp = (input()).lower()
alps = string.ascii_lowercase
l1 = []
for i in alps:
l1.append(i)
# print(l1, inp)
count = []
for i in inp:
try:
count.append(l1.index(i))
except ValueError:
count.append(" ")
# print(count)
str = ''
for i in count:
try:
str += l1[25-i]
except TypeError:
str += " "
print(str)