Why it is a Value error :substring not found? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

Why it is a Value error :substring not found?

Here is a new one https://code.sololearn.com/cmlkxWOmlHyG/?ref=app

23rd Jun 2021, 8:08 AM
Egor Tonchev(EGO)
Egor Tonchev(EGO) - avatar
13 Answers
+ 2
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
24th Jun 2021, 5:58 AM
Shadoff
Shadoff - avatar
+ 7
I applied it.
23rd Jun 2021, 6:31 PM
Egor Tonchev(EGO)
Egor Tonchev(EGO) - avatar
+ 7
When I write "sololearn" the result is correct, if I write "Vegetable pizza", ValueError is appeared.
24th Jun 2021, 5:47 AM
Egor Tonchev(EGO)
Egor Tonchev(EGO) - avatar
+ 7
Shadoff ,now it works fine. THANKS to all of You. ☺️👍
24th Jun 2021, 7:32 AM
Egor Tonchev(EGO)
Egor Tonchev(EGO) - avatar
+ 7
visph ,OK Thank You very much.
24th Jun 2021, 1:01 PM
Egor Tonchev(EGO)
Egor Tonchev(EGO) - avatar
+ 6
Lothar ,visph ,Shadoff, Juan Baños 🇪🇸 I've tried everything, but the problem continues to egxist .
23rd Jun 2021, 6:22 PM
Egor Tonchev(EGO)
Egor Tonchev(EGO) - avatar
+ 5
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!
23rd Jun 2021, 3:07 PM
Lothar
Lothar - avatar
+ 2
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)
23rd Jun 2021, 8:17 AM
visph
visph - avatar
+ 2
Ну работает же! по задумке это должно быть закодированное письмо с заменой букв на буквы по тому же номеру в алфавите только задом на перед?
24th Jun 2021, 4:24 AM
Shadoff
Shadoff - avatar
+ 1
the problem is here: message+=z[plc]
23rd Jun 2021, 8:42 AM
Shadoff
Shadoff - avatar
+ 1
Egor Tonchev(EGO) your actual code linked in question is empty ^^
23rd Jun 2021, 6:24 PM
visph
visph - avatar
+ 1
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)...
23rd Jun 2021, 6:35 PM
visph
visph - avatar
+ 1
""" 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)
24th Jun 2021, 12:48 PM
visph
visph - avatar