任何人都可以帮我找出什么是错的?我的代码中的索引错误

时间:2021-06-04 16:46:01

I am writing a very rough Caesar Cipher in Python and it works fine with simple messages, but when I enter the full alphabet in, I get a error on my 16th line, saying that there is an index error: string index out of range. Can anyone help me find out what's wrong? Here's my code:

我在Python中编写了一个非常粗略的Caesar Cipher,它可以很好地处理简单的消息,但是当我输入完整的字母表时,我的第16行出错,说有一个索引错误:字符串索引超出范围。任何人都可以帮我找出什么是错的?这是我的代码:

    abc = "ABCDEFGHIJKLMNOPQRTUVWXYZ"
    m = str(input("Message: "))
    m = m + "~"
    m_t = m.index("~")
    o = int(input("Offset: "))
    e_m = "Encrypted Message: "
    for loop_counter in range(m_t):
        c = m[loop_counter]
        if c in abc:
            p = abc.index(c)
            p = p + o
            if 25 < p:
                p = p - 26
            elif 0 > p:
                p = p + 26
            n_c = abc[p]
            e_m = e_m + n_c
        else:
            e_m = e_m + c

    print(e_m)

1 个解决方案

#1


Your 'abc' string is 25 in length, you missed the "S" letter...

你的'abc'字符串长度为25,你错过了“S”字母......

#1


Your 'abc' string is 25 in length, you missed the "S" letter...

你的'abc'字符串长度为25,你错过了“S”字母......