Help me with this code! | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 1

Help me with this code!

You are trying to send a secret message, and you've decided to encode it by replacing every letter in your message with its corresponding letter in a backwards version of the alphabet. What do your messages look like? Task: Create a program that replaces each letter in a message with its corresponding letter in a backwards version of the Engish alphabet. Input Format: A string of your message in its normal form. Output Format: A string of your message once you have encoded it (all lower case). Sample Input: Hello World Sample Output: svool dliow ___________________________________________ #this is the code i wrote for this problem and it's not working message = input() alphabets = "abcdefghijklmnopqrstuvwxyz" letters = "zyxwvutsrqponmlkjihgfedcba" for i in message: place = alphabets.index(i) print (letters[place])

2nd Feb 2021, 6:19 AM
thandava krishna
thandava krishna - avatar
2 ответов
+ 2
The problem is that when a character is not an alphabet, for example a whitespace or a digit, it will cause an error as it is not in alphabet variable. Another thing is to print the decoded characters right away, by indenting inside the for loop instead of changing the value of place every iteration. Fix done: - Use if-else statement inside for loop to know if a character is a letter - You can print each character rigjt away without a trailing new line, or concatenate each character to a variable to print later. If you have more questions, please feel free to ask. Thanks. https://code.sololearn.com/cQa97gHHbhb2/?ref=app https://code.sololearn.com/chgQW057hz2f/?ref=app
2nd Feb 2021, 6:31 AM
noteve
noteve - avatar
+ 1
Thank you
2nd Feb 2021, 6:52 AM
thandava krishna
thandava krishna - avatar