1

我是 Python 的初学者,所以我得到了错误

回溯(最近一次通话最后):

文件“C:/Users/spchee/Documents/cc.py”,第 43 行,c("helloz", 2) 文件“C:/Users/spchee/Documents/cc.py”,第 35 行,c add_to_string (n) 文件“C:/Users/spchee/Documents/cc.py”,第 5 行,在 add_to_string 中如果 message[i].titlecase():

IndexError:字符串索引超出范围

我目前的代码是:

import math
message = ""
i=0
def add_to_string(modifier):
    if message[i].titlecase():
        encrypted = encrypted + string[modifier].upper()
    else:
        encrypted = encrypted +string[modifier]

def c(message,key):



    string = "0abcdefghijklmnopqrstuvwxyz0"
    encrypted = ""
    legnth_message = len(message)
    i = 0


    for i in range (legnth_message-1):

        n = 0
        x = 0

        while message[i] != string[x].lower():
            x+=1;

        while string[int(x)+int(n)] != "0":
            n += math.copysign(1, key)

        if abs(key)<abs(n):
            n = (abs(n)-abs(key))*math.copysign(1,key)

            if math.copysign(1,key) == 1:
                add_to_string(n)
            else:
                add_to_string(27+n)

        else:
            add_to_string(x+key)


c("helloz", 2)

该代码应用作凯撒密码。提前致谢

4

0 回答 0