Python Forum
ValueError: substring not found
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ValueError: substring not found
#1
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()
Reply
#2
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']
Reply
#3
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  extract substring from a string before a word !! evilcode1 3 543 Nov-08-2023, 12:18 AM
Last Post: evilcode1
  [SOLVED] [regex] Why isn't possible substring ignored? Winfried 4 1,068 Apr-08-2023, 06:36 PM
Last Post: Winfried
  ValueError: substring not found nby2001 4 7,936 Aug-08-2022, 11:16 AM
Last Post: rob101
  Match substring using regex Pavel_47 6 1,426 Jul-18-2022, 07:46 AM
Last Post: Pavel_47
  ValueError: Found input variables with inconsistent numbers of samples saoko 0 2,475 Jun-16-2022, 06:59 PM
Last Post: saoko
  Substring Counting shelbyahn 4 6,129 Jan-13-2022, 10:08 AM
Last Post: krisputas
  Python Substring muzikman 4 2,314 Dec-01-2020, 03:07 PM
Last Post: deanhystad
  How can I found how many numbers are there in a Collatz Sequence that I found? cananb 2 2,548 Nov-23-2020, 05:15 PM
Last Post: cananb
  Removing items from list if containing a substring pythonnewbie138 2 2,203 Aug-27-2020, 10:20 PM
Last Post: pythonnewbie138
  Substring and If then Condition to create column Chandan 2 2,356 Jan-23-2020, 08:40 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020