gpt4 book ai didi

python - 无法理解为什么递归深度超过限制

转载 作者:太空宇宙 更新时间:2023-11-04 03:15:37 24 4
gpt4 key购买 nike

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

一个函数将字母表中的打乱字母放入 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

最佳答案

您可以使用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 中以分号结束行;这是非常不鼓励的,并且会使您的代码更难阅读。在大多数情况下,您还应该避免使用全局变量。

此外,如果您打算将其用作密码,您可能应该使用字典而不是列表

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'

关于python - 无法理解为什么递归深度超过限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36390077/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com