我有这个密码生成器,它从包含小写字母、大写字母和数字(不包括 0)的列表中计算出长度为 2 到 6 个字符的组合 - 总共 61 个字符。
我只需要显示已经创建的组合的百分比(步长为 5)。我尝试计算所选长度的所有组合,从该数字开始计算边界值(5% 步长值)并计算文本文件中写入的每个组合,当组合计数满足边界值时,打印xxx % completed
, 但是这段代码似乎不起作用。
请问您知道如何轻松显示百分比吗?
对不起我的英语,我不是母语人士。
谢谢你们!
def pw_gen(characters, length):
"""generate all characters combinations with selected length and export them to a text file"""
# counting number of combinations according to a formula in documentation
k = length
n = len(characters) + k - 1
comb_numb = math.factorial(n)/(math.factorial(n-length)*math.factorial(length))
x = 0
# first value
percent = 5
# step of percent done to display
step = 5
# 'step' % of combinations
boundary_value = comb_numb/(100/step)
try:
# output text file
with open("password_combinations.txt", "a+") as f:
for p in itertools.product(characters, repeat=length):
combination = ''.join(p)
# write each combination and create a new line
f.write(combination + '\n')
x += 1
if boundary_value <= x <= comb_numb:
print("{} % complete".format(percent))
percent += step
boundary_value += comb_numb/(100/step)
elif x > comb_numb:
break