Python 无法循环函数生成密码1000次
我有一个生成四个字母的密码的函数 但是,我无法循环函数生成1000次密码。它将代码打印两次,然后得到一个错误,指出列表索引超出范围Python 无法循环函数生成密码1000次,python,Python,我有一个生成四个字母的密码的函数 但是,我无法循环函数生成1000次密码。它将代码打印两次,然后得到一个错误,指出列表索引超出范围 import random base1 = ["A", "B", "C", "D"] base2 = ["E", "F", "G", "H"] def chooseOneLetter(base1, base2): ratio = 10 seed = int(random.uniform (0, ratio*len(base1)+len(base
import random
base1 = ["A", "B", "C", "D"]
base2 = ["E", "F", "G", "H"]
def chooseOneLetter(base1, base2):
ratio = 10
seed = int(random.uniform (0, ratio*len(base1)+len(base2)))
if seed < ratio*len(base1):
chosenLetter = base1[int(seed/ratio)]
base1.remove(chosenLetter)
else:
chosenLetter = base2[(seed - ratio*len(base1))]
base2.remove(chosenLetter)
return chosenLetter
def getSecretCode(base1, base2):
secretCode = ""
for i in range(4):
chosenLetter = chooseOneLetter (base1, base2)
secretCode += chosenLetter
return secretCode
def Code():
d = {}
base1 = ["A", "B", "C", "D"]
base2 = ["E", "F", "G", "H"]
for i in range(1000):
secretCode = getSecretCode(base1, base2)
print(secretCode)
Code()
随机导入
base1=[“A”、“B”、“C”、“D”]
base2=[“E”、“F”、“G”、“H”]
def选择一个字母(base1、base2):
比率=10
seed=int(random.uniform(0,比率*len(base1)+len(base2)))
如果种子<比率*len(基数1):
chosenLetter=base1[int(种子/比率)]
base1.移除(chosenLetter)
其他:
chosenLetter=base2[(种子比率*len(base1))]
base2.移除(chosenLetter)
回信
def getSecretCode(base1、base2):
secretCode=“”
对于范围(4)中的i:
chosenLetter=chooseOneLetter(base1,base2)
secretCode+=chosenLetter
返回密码
定义代码():
d={}
base1=[“A”、“B”、“C”、“D”]
base2=[“E”、“F”、“G”、“H”]
对于范围(1000)内的i:
secretCode=getSecretCode(base1,base2)
打印(机密代码)
代码()
输出:
DHCA
BGFE
Traceback (most recent call last):
File "main.py", line 36, in <module>
Code()
File "main.py", line 32, in Code
secretCode = getSecretCode(base1, base2)
File "main.py", line 21, in getSecretCode
chosenLetter = chooseOneLetter (base1, base2)
File "main.py", line 14, in chooseOneLetter
chosenLetter = base2[(seed - ratio*len(base1))]
IndexError: list index out of range
DHCA
BGFE
回溯(最近一次呼叫最后一次):
文件“main.py”,第36行,在
代码()
文件“main.py”,第32行,代码
secretCode=getSecretCode(base1,base2)
getSecretCode中第21行的文件“main.py”
chosenLetter=chooseOneLetter(base1,base2)
文件“main.py”,第14行,在chooseOneLetter中
chosenLetter=base2[(种子比率*len(base1))]
索引器:列表索引超出范围
问题在于,您正在从列表中删除字母而不替换它们。base1和base2的长度变为0,因此抛出索引错误。只需将for循环移到列表指定的上方
def Code():
d = {}
for i in range(1000):
base1 = ["A", "B", "C", "D"]
base2 = ["E", "F", "G", "H"]
secretCode = getSecretCode(base1, base2)
print(secretCode)
问题是您正在从列表中删除字符,因此在两次迭代后,您尝试访问一个超出边界的索引。 Iv'e添加了
在选择一封信
功能中尝试除此之外的操作:
def chooseOneLetter(base1, base2):
ratio = 10
seed = int(random.uniform (0, ratio*len(base1)+len(base2)))
if seed < ratio*len(base1):
try:
chosenLetter = base1[int(seed/ratio)]
except:
print("base1",base1)
base1.remove(chosenLetter)
else:
try:
chosenLetter = base2[(seed - ratio*len(base1))]
except:
print("base2",base2)
base2.remove(chosenLetter)
return chosenLetter
意思是base2
为空
要解决此问题,请将base1
和base2
移动到for
循环中,如下所示:
def Code():
d = {}
for i in range(1000):
base1 = ["A", "B", "C", "D"]
base2 = ["E", "F", "G", "H"]
secretCode = getSecretCode(base1, base2)
print(secretCode)
总之,问题是你正在处理相同的列表,因此“用完”了信件。通过在每次迭代中创建列表,可以避免该问题
但是,将base1
和base2
发送1000次到chooseOneLetter
当然不是那么有效。因此,可以考虑在函数内移动它们的定义。这样可以避免每次创建和发送它们
def Code():
d = {}
for i in range(1000):
base1 = ["A", "B", "C", "D"]
base2 = ["E", "F", "G", "H"]
secretCode = getSecretCode(base1, base2)
print(secretCode)