为什么这个替换密码脚本会引发错误?

时间:2015-04-21 08:50:07

标签: python python-2.7 encryption runtime-error local-variables

我对python很新,所以请耐心等待。我正在组合一个小的加密拼图,其中一部分需要替换密码。在这种情况下,每个字母用三个随机字母替换。每次运行此代码时,当我尝试加密某些内容时都会抛出错误:

Traceback (most recent call last):
File "/home/pi/Desktop/expansion cipher.py", line 159, in <module>
crypt()
File "/home/pi/Desktop/expansion cipher.py", line 142, in crypt
print encrypt(txt)
File "/home/pi/Desktop/expansion cipher.py", line 127, in encrypt
ctxt = ctxt + exp_ciph(ptxt[counter])
File "/home/pi/Desktop/expansion cipher.py", line 121, in exp_ciph
return cur_exp
UnboundLocalError: local variable 'cur_exp' referenced before assignment

这是我的代码:

def rev_ciph(char):
    if char == "gps":
        cur_rev = "_"
    if char == "evl":
        cur_rev = "."
    if char == "jkb":
        cur_rev = "e"
    if char == "bhj":
        cur_rev = "t"
    if char == "szk":
        cur_rev = "a"
    if char == "nwu":
        cur_rev = "o"
    if char == "dpl":
        cur_rev = "i"
    if char == "sbg":
        cur_rev = "n"
    if char == "dsl":
        cur_rev = "s"
    if char == "yhq":
        cur_rev = "p"
    if char == "sav":
        cur_rev = "h"
    if char == "gfs":
        cur_rev = ","
    if char == "rtg":
        cur_rev = "d"
    if char == "fqu":
        cur_rev = "l"
    if char == "rjt":
        cur_rev = "u"
    if char == "sbv":
        cur_rev = "c"
    if char == "yqm":
        cur_rev = "m"
    if char == "ywh":
        cur_rev = "f"
    if char == "drt":
        cur_rev = "y"
    if char == "zfd":
        cur_rev = "w"
    if char == "asn":
        cur_rev = "g"
    if char == "fzj":
        cur_rev = "p"
    if char == "rka":
        cur_rev = "b"
    if char == "kzv":
        cur_rev = "v"
    if char == "lah":
        cur_rev = "k"
    if char == "rma":
        cur_rev = "x"
    if char == "fqn":
        cur_rev = "q"
    if char == "vrq":
        cur_rev = "j"
    if char == "tfv":
        cur_rev = "z"
    return cur_rev

def exp_ciph(char):
    if char == "_":
        cur_exp = "gps"
    if char == ".":
        cur_exp = "evl"
    if char == "e":
        cur_exp = "jkb"
    if char == "t":
        cur_exp = "bhj"
    if char == "a":
        cur_exp = "szk"
    if char == "o":
        cur_exp = "nwu"
    if char == "i":
        cur_exp = "dpl"
    if char == "n":
        cur_exp = "sbg"
    if char == "s":
        cur_exp = "dsl"
    if char == "p":
        cur_exp = "yhq"
    if char == "h":
        cur_exp = "sav"
    if char == ",":
        cur_exp = "gfs"
    if char == "d":
        cur_exp = "rtg"
    if char == "l":
        cur_exp = "fqu"
    if char == "u":
        cur_exp = "rjt"
    if char == "c":
        cur_exp = "sbv"
    if char == "m":
        cur_exp = "yqm"
    if char == "f":
        cur_exp = "ywh"
    if char == "y":
        cur_exp = "drt"
    if char == "w":
        cur_exp = "zfd"
    if char == "g":
        cur_exp = "asn"
    if char == "p":
        cur_exp = "fzj"
    if char == "b":
        cur_exp = "rka"
    if char == "v":
        cur_exp = "kzv"
    if char == "k":
        cur_exp = "lah"
    if char == "x":
        cur_exp = "rma"
    if char == "q":
        cur_exp = "fqn"
    if char == "j":
        cur_exp = "vrq"
    if char == "z":
        cur_exp = "tfv"
    return cur_exp

def encrypt(ptxt):
    ctxt = "Ciphertext: "
    counter = 0
    while counter <= len(ptxt):
        ctxt = ctxt + exp_ciph(ptxt[counter])
        counter += 1
    return ctxt

def decrypt(ctxt):
    ptxt = "Plaintext: "
    counter = 0
    while counter <= len(ctxt):
        ptxt = ptxt + rev_ciph(ctxt[counter])
        counter += 1
    return ptxt

def crypt():
    print
    txt = raw_input("Plaintext: ")
    print encrypt(txt)
    print

def ucrypt():
    print
    txt = raw_input("Ciphertext: ")
    print decrypt(txt)
    print

ex_code = False
while ex_code == False:
    print "(1) Encrypt"
    print "(2) Decript"
    print "(3) Exit"
    print
    mchoc = raw_input("What would you like to do(1,2,3?): ")
    if mchoc == "1":
        crypt()
    if mchoc == "2":
        ucrypt()
    if mchoc == "3":
        ex_code = True
    print

1 个答案:

答案 0 :(得分:0)

你有两个问题。首先,您不能处理所有可能的输入。刚刚复制并测试了您的代码后,我发现只有当您尝试加密包含UnboundLocalError: local variable 'cur_exp' referenced before assignment(或exp_ciph(char)未处理的字符的内容时才会获得rev_ciph(char) if } 对于这个问题)。例如,这些函数都不处理大写字母。 Python总是区分大小写,并将大写和小写字母视为完全不同的字符。您的功能也不会处理其他字符,例如&#34;,&#34;和&#34;;&#34;。如果为程序提供一个要加密的字符串,其中包含前两个函数未处理的字符,那么,当该字符出现时,这些函数中cur_exp语句的所有条件都为false,因此{永远不会创建{1}}并为其分配值。当其中一个函数尝试return cur_exp时,它会发现cur_exp不存在。这是您在上面描述的错误的来源。

如果你输入的字符串只包含小写字母和可能的&#34; _&#34;,你会得到一个完全不同的错误,即:

IndexError: string index out of range

这是因为函数encrypt(ptxt)总是寻找一个字符而不是字符串。在Python(以及我认为,每种编程语言)中,可迭代中的元素(如字符串或列表)由OFFSET从第一个项目索引;也就是说,前面有多少项目。所以,如果你有一个包含4个字符的字符串foo

foo = "abcd"

然后:

foo[0] == "a"
foo[1] == "b"
foo[2] == "c"
foo[3] == "d"

没有foo[4]。调用foo[4]将引发上述错误。 len(object)函数返回iterable中的项数。所以,按照上面的例子,

len(foo) == 4

然后调用

foo[len(foo)]

将提升IndexError:。这完全是encrypt(ptxt)函数

中经常出现的情况
counter = 0
while counter <= len(ptxt):
    ctxt = ctxt + exp_ciph(ptxt[counter])
    counter += 1

您需要这样做:

counter = 0
while counter <= len(ptxt) - 1:
    ctxt = ctxt + exp_ciph(ptxt[counter])
    counter += 1

这是我遇到的第一个遇到的问题。我不能保证不会再有了。希望这会让你走得更远。