传递参数和范围的问题,我该如何解决这个问题?

问题描述 投票:1回答:3

我正在制作密码生成器和强度检查器(我是编码新手),我的问题是我无法获得生成密码的函数,调用检查强度的函数,并进行强度检查函数返回生成函数。

很抱歉,如果这是一个不好的解释,请检查代码以获得澄清。

我尝试过的所有东西都已经部分工作或根本不工作,包括使用全局变量;即便如此,我也无法让它正常运作。

import random
allchars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!$%^&*()_-=+"
def generatePassword():
    generatedPass = ""
    while points < 20:
        for i in range(random.randint(8, 12)):
            generatedPass += random.choice(allchars)
        checkFunc(generatedPass)
    points = 0

    print(generatedPass)


def checkFunc(password):
    numberofTriples = 0
    consecChars = []
    points = 0
    allCrit = 0
    (Redacted code that just calculates the points of the password)
    (Redacted code that just calculates the points of the password)
    return points

我想让它随机生成密码,并检查其强度,如果它低于某个点阈值,生成另一个,直到它高于阈值,并打印它。

python python-3.x
3个回答
1
投票

你的generatedPassword函数不会在points循环中设置while的值,因此它永远不会失败points < 20的条件。

你想把checkFunc(generatedPass)改为points = checkFunc(generatedPass)。这将正确设置点的值,并从循环中断开。


1
投票

所以这里有你的代码的几个问题:

import random
allchars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!$%^&*()_-=+"
def generatePassword():
    #note you should have initiated ***points*** to 0 before using it in your while loop
    generatedPass = ""
    while points < 20:
        for i in range(random.randint(8, 12)):
            generatedPass += random.choice(allchars)
        checkFunc(generatedPass)
    points = 0 #points here has no value assignment you want to assign checkFunc()'s return value to points like this:   points = checkFunc(generatedPass), or else you'll have an infinite loop. 

    print(generatedPass)


def checkFunc(password):
    numberofTriples = 0
    consecChars = []
    points = 0
    allCrit = 0
##    (Redacted code that just calculates the points of the password)
##    (Redacted code that just calculates the points of the password)

     #Just be sure you have points variable here being updated according to the strength before you return it. 

以下是相同代码的示例,但我们正在检查长度而不是强度:

import random
allchars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!$%^&*()_-=+"
def generatePassword():
    generatedPass = ""
    points = 0 
    while points < 20:
        for i in range(random.randint(8, 12)):
            generatedPass += random.choice(allchars)
        points = checkLength(generatedPass)


    print(generatedPass)


def checkLength(password):

    return len(password)

输入:

generatePassword()

输出:

1Ad0%7c0En


0
投票

我希望你做得很好。

这是一个应该有效的更正代码:

更正完成:

  • points在使用前未申报
  • 调用checkFunc后points没有更新

N.B:为了模拟得分的计算,我使用了随机函数。

这是完整的代码:

import random
allchars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!$%^&*()_-=+"


def generatePassword():
    generatedPass = ""
    # We set points to 0 to start the while loop
    points = 0
    while points < 20:
        for i in range(random.randint(8, 12)):
            generatedPass += random.choice(allchars)
        # We set points to the score computed by checkFunc to know if we need to trigger the generation of a new password.
        points = checkFunc(generatedPass)

    print(generatedPass)


def checkFunc(password):
    numberofTriples = 0
    consecChars = []
    points = 0
    allCrit = 0
    # Simulating point computation
    points = random.randint(10,30)
    return points

我希望它有所帮助,祝你有个美好的一天,

G

© www.soinside.com 2019 - 2024. All rights reserved.