无法理解为什么递归深度超出限制

时间:2016-04-03 19:27:38

标签: python recursion

我有一个程序是一个简单的加密算法,它接收一个字符串并将字母更改为一个新的字母串。

函数将字母表中的加扰字母放入code表中。在这个函数中,我有checkRepeat函数来确保字母不重复。当我手动执行此操作时(除随机整数生成部分之外)它是有意义的,但我的计算机不喜欢它并且超过了递归深度'。

from random import *;

string="Hello I am a computer";
alphTable=['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'];
def checkRepeat(array,val):
    global alphTable
    for i in range(len(array)):
        if val==array[i]:
            location=randint(0,25);
            array.append(alphTable[location]);
            checkRepeat(array,val);

def makeEncryptTable():
    encryptTable=[];
    global alphTable;
    for i in range (26):
        location=randint(0,25);
        encryptTable.append(alphTable[location]);
        checkRepeat(encryptTable,encryptTable[i]);
    return encryptTable;

array1=makeEncryptTable();
print(array1);

以下是错误代码:

Traceback (most recent call last):
  File "/home/brandon/Documents/CG 301/WilliamsBrandonStringEncrpytionAlgorithm.py", line 38, in <module>
    array1=makeEncryptTable();
  File "/home/brandon/Documents/CG 301/WilliamsBrandonStringEncrpytionAlgorithm.py", line 35, in makeEncryptTable
    checkRepeat(encryptTable,encryptTable[i]);
  File "/home/brandon/Documents/CG 301/WilliamsBrandonStringEncrpytionAlgorithm.py", line 27, in checkRepeat
    checkRepeat(array,val);

...

File "/home/brandon/Documents/CG 301/WilliamsBrandonStringEncrpytionAlgorithm.py", line 27, in checkRepeat
    checkRepeat(array,val);
  File "/home/brandon/Documents/CG 301/WilliamsBrandonStringEncrpytionAlgorithm.py", line 25, in checkRepeat
    location=randint(0,25);
  File "/usr/lib/python3.4/random.py", line 218, in randint
    return self.randrange(a, b+1)
  File "/usr/lib/python3.4/random.py", line 194, in randrange
    return istart + self._randbelow(width)
  File "/usr/lib/python3.4/random.py", line 228, in _randbelow
    if type(random) is BuiltinMethod or type(getrandbits) is Method:
RuntimeError: maximum recursion depth exceeded while calling a Python object

2 个答案:

答案 0 :(得分:1)

您可以使用randomstring

轻松完成此操作
import random
import string

encrypt_table = list(string.ascii_lowercase)
print encrypt_table
# ['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']

random.shuffle(encrypt_table)
print encrypt_table
# ['d', 'v', 'f', 't', 'c', 'r', 's', 'q', 'e', 'u', 'm', 'w', 'p', 'g', 'x', 'i', 'b', 'n', 'z', 'y', 'k', 'h', 'a', 'o', 'l', 'j']

另外,你不需要在使用分号的python中结束行;它非常沮丧,使你的代码更难阅读。在大多数情况下,您还应该避免使用全局变量。

此外,如果您打算将其用作密码,则应该使用dictionary代替list

cipher = {k: v for k, v in zip(string.ascii_letters, encrypt_table}
word = 'test'
encrypted = ''.join(cipher[x] for x in word)
# 'yczy'

或者,只需使用内置的密码工具。

cipher = string.maketrans(string.ascii_lowercase, encrypt_table)
string.translate('test', cipher)
# 'yczy'

答案 1 :(得分:1)

您的checkRepeat(array,val)没有退出条件。

您每次都使用相同的checkRepeat(array,val)调用val并且它匹配数组中的第一个元素,将另一个元素添加到数组并重复匹配第一个元素并附加另一个元素