Python Forum
ValueError: substring not found - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: ValueError: substring not found (/thread-21297.html)



ValueError: substring not found - hoangthai10788 - Sep-23-2019

Hello,

I encountered this issue when self-learning Python. Could anyone give me a full explanation what is happening here? Any help would be greatly appreciated. Thank you!

def shift(char, key):
    alphabet = "abcdefghijklmnopqrstuvwxyz"
    updated_alphabet = ""
    if int(key) > 0:
        for i in range(len(alphabet)):
            updated_alphabet += alphabet[(int(key) + i) % 26]
    char_index = alphabet.index(char.lower())
    new_char = updated_alphabet[char_index]
    return new_char
def main():
    message = "Please help me with this."
    new_mess = ""
    for char in message:
        new_mess += shift(char, 1)
    return new_mess
main()



RE: ValueError: substring not found - Axel_Erfurt - Sep-23-2019

alphabet is a single string

from_letter = 'a'
to_letter = 'z'
alphabet = [chr(c) for c in range(ord(from_letter), ord(to_letter))]
alphabet.append("z")
print(alphabet)
Output:
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']



RE: ValueError: substring not found - ichabod801 - Sep-23-2019

It's helpful if you explain the issue you're having. If you'd given the full text of the error message it would have been much easier to diagnose the problem.

In any case, you don't have ' ' and '.' in your alphabet. So when you try to convert them you get an error. Given that this is classical cryptography, the standard method is to remove all non-alphabetic characters, and then split the output into blocks of five after encoding.