Python2.7跳过函数中的循环

2024-06-08 08:32:12 发布

您现在位置:Python中文网/ 问答频道 /正文

这是一个简单的程序来加密和解密一些文本。 但它似乎并不像预期的那样起作用。。 我也被告知无论如何都要使用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


Tags: theinforinputlenifdefbe
1条回答
网友
1楼 · 发布于 2024-06-08 08:32:12

我已经运行了脚本。这就是你所期待的结果吗

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时,它找不到任何匹配的对象,因此它返回空值,int(enck[x])充满了Nones

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

你可以在这里看到无: enter image description here

所以,你可以做的一件事,就是正确定义你的字母空间,包括上下两个字母

相关问题 更多 >