Python - 我收到一条错误消息 'substring not found'

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

我正在尝试为class项目制作一个换位密码加密函数。

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

我收到错误 'substring not found',是我做错了什么吗?

如果我的输入是 =(‘SLOTH POWER’) 对于 s,(‘TOP’) 对于键,我的输出应该是:‘RLOTPOHWES’

还有没有其他的限制函数为ord(), len(), range()?如果是这样,我也可以展示一下吗?

错误:

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

找不到ascii_lowercase中的字符,因为你输入的是大写字母。尝试使用“sloth power”而不是“SLOTH POWER”,或使用 s.lower().