传递参数和范围时出现问题,我该如何解决?

时间:2019-03-22 22:07:02

标签: python python-3.x

我正在制作密码生成器和强度检查器(我是编码的新手),我的问题是我无法让函数工作以生成密码,调用用于检查强度的函数并具有强度检查功能返回到生成功能。

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

我尝试过的所有方法都部分起作用或根本不起作用,包括使用全局变量;即使那样,我也无法正常运行。

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

我希望它采用它随机生成的密码,并检查它的强度(如果它低于某个特定点阈值),以生成另一个密码,直到它超过阈值并打印。

3 个答案:

答案 0 :(得分:1)

您的generatedPassword函数从未在points循环中设置while的值,因此它永远不会失败points < 20的情况。

您想要将checkFunc(generatedPass)更改为points = checkFunc(generatedPass)。这样可以正确设置点的值,并从循环中断开。

答案 1 :(得分: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()

    
  

输出:

  
    
      

1Aid0%7tZYo0Ip(u_zeCQo = I

    
  

答案 2 :(得分:0)

我希望你做得很好。

以下是正确的代码,可以正常工作:

已更正:

  • points在使用前未声明
  • points在调用checkFunc之后未更新

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