0

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’

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
3
  • Please edit your post and show the full text of the traceback. Please also correct your indentation, as your code is not runnable as is. Commented Apr 10, 2022 at 17:04
  • apologizes for the incorrect indentation. Commented Apr 10, 2022 at 17:09
  • maybe first use print() to see what you have in key_chr If you have upper case char then you can't find it in ascii_lowercase Commented Apr 10, 2022 at 17:24

1 Answer 1

3

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().

Sign up to request clarification or add additional context in comments.

12 Comments

or it needs ascii_uppercase
thank you. I have another question. Is there also another way to limit the functions to ord(), len(), and range() in making the transposition function? If so, could I be shown how?
If I understand you correctly, you want to simplify your code and replace part of it with the built in functions?
yes, I tried doing so, but I got lost where I tried to define my alphabet and assign it to the indexes of the message and keyword.
To simplify the swap function use the code from stackoverflow.com/a/25954716/14079544
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.