Python 传递参数和作用域时出现问题,如何解决此问题?
我正在制作一个密码生成器和强度检查器(我不熟悉编码),我的问题是我无法让一个生成密码的函数工作,调用检查强度的函数,并让强度检查函数返回到生成函数 抱歉,如果这是一个错误的解释,请检查代码进行澄清 我尝试过的每件事都部分有效或根本无效,包括使用globals;即使那样,我也不能让它正常工作Python 传递参数和作用域时出现问题,如何解决此问题?,python,python-3.x,Python,Python 3.x,我正在制作一个密码生成器和强度检查器(我不熟悉编码),我的问题是我无法让一个生成密码的函数工作,调用检查强度的函数,并让强度检查函数返回到生成函数 抱歉,如果这是一个错误的解释,请检查代码进行澄清 我尝试过的每件事都部分有效或根本无效,包括使用globals;即使那样,我也不能让它正常工作 import random allchars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!$%^&*()_-=+
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
随机导入
allchars=“abcdefghijklmnopqrstuvxyzabcdefghijklmnopqrstuvxyz012456789!$%^&*()_-=+”
def generatePassword():
generatedPass=“”
而<20分:
对于范围内的i(random.randint(8,12)):
generatedPass+=random.choice(所有字符)
checkFunc(生成过程)
分数=0
打印(生成通行证)
def checkFunc(密码):
numberofTriples=0
concerchars=[]
分数=0
allCrit=0
(仅计算密码点数的修订代码)
(仅计算密码点数的修订代码)
返回点
我希望它随机生成密码,并检查其强度,如果它低于某个点阈值,则生成另一个密码,直到它高于阈值,然后打印出来。我希望你做得很好 下面是一个正确的代码,应该可以工作: 所做更正:
在使用前未声明点
- 调用checkFunc后,
未更新点
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
随机导入
allchars=“abcdefghijklmnopqrstuvxyzabcdefghijklmnopqrstuvxyz012456789!$%^&*()_-=+”
def generatePassword():
generatedPass=“”
#我们将点设置为0以启动while循环
分数=0
而<20分:
对于范围内的i(random.randint(8,12)):
generatedPass+=random.choice(所有字符)
#我们将点设置为checkFunc计算的分数,以了解是否需要触发新密码的生成。
点=checkFunc(生成过程)
打印(生成通行证)
def checkFunc(密码):
numberofTriples=0
concerchars=[]
分数=0
allCrit=0
#模拟点计算
点=随机。随机数(10,30)
返回点
我希望有帮助,
祝你有愉快的一天
G您的
生成密码
函数在循环时不会设置点
的值,因此它永远不会不满足点<20
的条件
您需要将checkFunc(generatedPass)
更改为points=checkFunc(generatedPass)
。这将正确设置点的值,并中断循环。因此,您的代码存在以下几个问题:
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.
随机导入
allchars=“abcdefghijklmnopqrstuvxyzabcdefghijklmnopqrstuvxyz012456789!$%^&*()_-=+”
def generatePassword():
#注意:在while循环中使用***点之前,您应该先将***点***初始化为0
generatedPass=“”
而<20分:
对于范围内的i(random.randint(8,12)):
generatedPass+=random.choice(所有字符)
checkFunc(生成过程)
points=0#此处的points没有赋值您要将checkFunc()的返回值赋值给这样的点:points=checkFunc(generatedPass),否则将有一个无限循环。
打印(生成通行证)
def checkFunc(密码):
numberofTriples=0
concerchars=[]
分数=0
allCrit=0
##(仅计算密码点数的修订代码)
##(仅计算密码点数的修订代码)
#只需确保在返回之前,根据强度更新此处的点变量。
下面是一个相同代码的示例,但我们检查的不是强度,而是长度:
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)
随机导入
allchars=“abcdefghijklmnopqrstuvxyzabcdefghijklmnopqrstuvxyz012456789!$%^&*()_-=+”
def generatePassword():
generatedPass=“”
分数=0
而<20分:
对于范围内的i(random.randint(8,12)):
generatedPass+=random.choice(所有字符)
点=检查长度(生成过程)
打印(生成通行证)
def checkLength(密码):
返回len(密码)
输入:
generatePassword()
输出:
ID0%7tZYo0Ip(u_zeCQo=I
您好,Anss M,这段代码运行吗?IINM您在生成密码
函数中初始化之前检查点的值。我忘记添加了,程序中的函数外,点已经设置为0。您的问题实际上不包含任何问题或错误,因此我不确定我们可以如何帮助您。它如果您检查堆栈溢出的、、和,那就好了。