Python 2.7 跳过函数中的for循环

Python 2.7 skipping for loops in a function

这是一个加密和解密一些文本的简单程序。 但它似乎没有按预期工作.. 我也被告知无论如何都要使用 2.7 感谢您的帮助,谢谢。

Python 2.7.16 中的代码:

letterspace = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 
'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', ' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
input1e = ""
keyse = ""
input1d = ""
keysd = ""

def inpute():
    global input1e
    global keyse
    input1e = raw_input("Enter the message to be encrypted : ")
    input1e = input1e.upper()
    #keyinput = "Input the key (Key should be of length " + str(len(input1e)) + ") : "
    keyse = raw_input("Enter the key : ")
    if len(keyse) == len(input1e):
        return
    else:
        print("Please input the values correctly!")
        inpute()

def inputd():
    global input1d
    global keysd
    input1d = raw_input("Enter the message to be decrypted : ")
    input1d = input1d.upper()
    #keyinput = "Input the key (Key should be of length " + str(len(input1d)) + ") : "
    keysd = raw_input("Enter the key : ")
    if len(keysd) == len(input1d):
        return
    else:
        print("Please input the values correctly!")
        inputd()

def conv(x):
    for k, v in enumerate(letterspace):
        if v == x:
            print(k)
            return k
        else:
            continue

def reconv(y):
    for k, v in enumerate(letterspace):
        if k == y:
            print(v)
            return str(v)
        else:
            continue

def encryptor(inpe, keye):
    encm = []
    enck = []
    encf = []
    ence = ""
    for i in inpe:
        encm.append(conv(i))

    for k in keye:
        enck.append(conv(k))

    for x in range(0,len(encm)):
        z = int(encm[x]) + int(enck[x])
        if z > 26:
            z -= 26
        encf.append(z)

    for w in encf:
        ence += reconv(w)

    print ence

def decryptor(inpd, keyd):
    decm = []
    deck = []
    decf = []
    dece = ""
    for i in inpd:
        decm.append(conv(i))

    for k in keyd:
        deck.append(conv(k))

    for x in range(0,len(decm)):
        z = decm[x] - deck[x]
        if z < 0:
            z += 26
        decf.append(z)

    for w in decf:
        dece += reconv(w)

    print dece

def menu():
    print("---------------------ONE-TIME-PAD-PROGRAM---------------------")
    sel = raw_input("Type E for Encryption or D for Decryption :")
    if len(sel) > 1:
        sel = sel[0]
    if sel == "E" or sel == "e":
        inpute()
        encryptor(input1e, keyse)

    elif sel == "D" or sel == "d":
        inputd()
        decryptor(input1d, keyse)

    else:
        print("Please enter correct value!")
        menu()

menu()

输出:A为0,B为1,依此类推..

---------------------ONE-TIME-PAD-PROGRAM---------------------
Type E for Encryption or D for Decryption :e
Enter the message to be encrypted : welcome
Enter the key : pragyan
22
4
11
2
14
12
4

Traceback (most recent call last):
  File "C:/Users/mnsan/Downloads/Pri/OTP.py", line 110, in <module>
    menu()
  File "C:/Users/mnsan/Downloads/Pri/OTP.py", line 100, in menu
    encryptor(input1e, keyse)
  File "C:/Users/mnsan/Downloads/Pri/OTP.py", line 61, in encryptor
    z = int(encm[x]) + int(enck[x])
TypeError: int() argument must be a string or a number, not 'NoneType'

它跳过了第 57 行的第二个 for 循环。 我的想法:https://en.wikipedia.org/wiki/One-time_pad

我有 运行 脚本。这就是你等待的结果吗?

Type E for Encryption or D for Decryption :e
Enter the message to be encrypted : WELCOME
Enter the key : PRAGYAN
22
4
11
2
14
12
4
15
17
0
6
24
0
13
L
V
L
I
M
M
R
LVLIMMR

问题是你的字母空间是大写的,而你输入的是小写字母。因此,当它尝试使用函数 conv 时,它找不到任何匹配项,因此它 returns 为空,而 int(enck[x]) 充满了 Nones。

第一个词没有问题,因为它只是数字。

你可以在这里看到无:

因此,您可以做的一件事是正确定义字母空间,包括大写字母和小写字母。