Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python – I am getting an error that says 'substring not found'

I am trying to make a transposition cipher encryption function for a class project.

from string import ascii_lowercase

def swap(s: str, index0: int, index1: int):
    smaller = index0 if index0 < index1 else index1
    bigger = index0 if index0 >= index1 else index1
    if bigger >= len(s) or smaller < 0:
        return None
    ret = s[:smaller] + s[bigger] + s[smaller+1:]  # swap first
    ret = ret[:bigger] + s[smaller] + s[bigger+1:] # swap second
    return ret


def swap_encrypt(s: str, key:str):
    ret = s
    for key_chr in key:
        index = ascii_lowercase.index(key_chr)
        swap_this = index % len(ret)
        with_this = (swap_this + 1) % len(ret)
        ret = swap(ret, swap_this, with_this)

    return ret
s = ''
key = ''
def main2():
    s = input('Enter your message: ')
    s = cleanup(s)
    key = input('Enter your keyword: ')
    key = cleanup(key)
    ret= swap_encrypt((s), (key))
    print(cleanup(ret))

main2()

I am getting the error ‘substring not found’, is there something I am doing wrong?

If my input is =(‘SLOTH POWER’) for s, (‘TOP’) for the key, my output should be: ‘RLOTPOHWES’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is there also another to limit the functions to ord(), len(), and range()? If so, could I be shown how as well?

error:

Traceback (most recent call last):
  File "c:\Users\darks\OneDrive\Documents\7\ciphers.py", line 139, in <module>
    main2()
  File "c:\Users\darks\OneDrive\Documents\7\ciphers.py", line 136, in main2
    ret= swap_encrypt((s), (key))
  File "c:\Users\darks\OneDrive\Documents\7\ciphers.py", line 123, in swap_encrypt
    index = ascii_lowercase.index(key_chr)
ValueError: substring not found

>Solution :

It can’t find the character in the ascii_lowercase, because your input is uppercase. Try "sloth power" instead of "SLOTH POWER", or use s.lower().

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading