Secret Message | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Secret Message

Could I solve It with re? https://code.sololearn.com/ciVha3KUphUX/?ref=app

20th Mar 2021, 3:55 PM
Giovanni Reale
Giovanni Reale - avatar
5 Answers
+ 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]
20th Mar 2021, 4:04 PM
visph
visph - avatar
+ 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 ;)
20th Mar 2021, 4:07 PM
visph
visph - avatar
+ 2
You can make your dict_encoder like this instead and replace everything between alphabet = [......] dict_encoder = dict(zip(alphabet, alphabet[::-1])) string = input()
20th Mar 2021, 6:10 PM
ChaoticDawg
ChaoticDawg - avatar
28th Apr 2021, 1:09 PM
Mr.Dark
Mr.Dark - avatar
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)
8th Jan 2022, 3:16 PM
Viren Shah
Viren Shah - avatar