13 AnswersNew Answer
because " " (space) is not in the list! change for loop section of your code to the following: for ltr in msg: if ltr in alphnew: place=alph.index(ltr) message += alphnew[place] else: message+= ltr
I applied it.
When I write "sololearn" the result is correct, if I write "Vegetable pizza", ValueError is appeared.
Shadoff ,now it works fine. THANKS to all of You. ☺️👍
visph ,OK Thank You very much.
Egor Tonchev(EGO) , i saw your last version, but there is still an issue with it. you have added a space to the alphabet variable at index 0, then this variable will be reversed. that is ok for all letters but not for spaces. a space has to be "translated" to space again. so the space in regular alphabet is at index 0, and in reversed in index 26. but the space in both strings has to be in the same index. to get rid of this issue you can do: ... alp= " abcdefghijklmnopqrstuvwxyz" # ok alpn = ' ' + alp[::-1] # <<<<<< ... then it should work. happy coding!
Lothar ,visph ,Shadoff, Juan Baños 🇪🇸 I've tried everything, but the problem continues to egxist .
error comes when index() did not found the argument string in thd target string: 'alphabet' not contains the char provided... you must: • test if ltr is a letter • make the letter uppercase • check the 'alphabet value': at least E is not inside (but you have twice I)
Ну работает же! по задумке это должно быть закодированное письмо с заменой букв на буквы по тому же номеру в алфавите только задом на перед?
the problem is here: message+=z[plc]
Egor Tonchev(EGO) your actual code linked in question is empty ^^
your code works fine if you only provide lowercase letter as input... you still have to test if each char is a letter (and just append it to result if not), turn it to lowercase if uppercase (and maybe turn the result char to uppercase)...
""" Egor Tonchev(EGO) I said you from start ^^ however, not all works fine in your actual code: 'a' is converted to a space... wich I think is not expected, 'b' to 'z', 'c' to 'y'... I guess you rather want 'a' to 'z', 'b' to 'y', 'c' to 'x'... and maybe keep lower/upper case: """ msg = input() alph = "abcdefghijklmnopqrstuvwxyz" alphnew = alph[::-1] message = "" for ltr in msg: if ltr.isalpha(): up = ltr.upper()==ltr place = alph.index(ltr.lower()) ltr = alphnew[place] if up: ltr = ltr.upper() message += ltr print(message)