Python程序在生成随机数索引lis时挂起

2024-05-19 01:13:25 发布

您现在位置:Python中文网/ 问答频道 /正文

这个问题现在几乎没用了。请忽略它。我为浪费别人的时间指出自己的粗心而感到极为尴尬。在

系统64-bit Windows 10 machine

Python安装Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32

编辑器:来自sublime text 3的原始代码,现在正在VS代码上编辑[最新稳定版本]

我对python相当陌生,一直在研究一个在cmd中运行的密码管理器程序。在

我使用了我以前做的一个函数来生成随机字符串[密码],并通过主脚本存储在一个文件中。它从另一个py文件导入到主脚本中。 代码挂在while count下面的第二行代码上!=length loop[来自导入脚本的代码] 完全相同的代码行以前也起作用 抱歉,示例代码太长了。在

length:由用户输入定义的变量;转换为int以供以后使用

计数:在此循环之前,变量设置为0

字符类型:一些字符列表[大写、小写、符号、数字]

incl_upp = False
incl_low = False
incl_int = False
incl_sym = False
checklist = []
char_types = []
password = []
length = input('# input length of password in integers | ')
if input('# include upper case alpha? (y/n) | ') == 'y':
    incl_upp = True
    print("# password now includes upper case alpha")
if input('# include lower case alpha? (y/n) | ') == 'y':
    incl_low = True
    print("# password now includes lower case alpha")
if input('# include integers? (y/n) | ') == 'y':
    incl_int = True
    print("# password now includes integers")
if input('# include special characters? (y/n) | ') == 'y':
    print("# password now includes special characters")
    incl_sym = True
if incl_upp == True:
    char_types.append(UPPER_CASE)
    checklist.append(UPPER_CASE)
if incl_low == True:
    char_types.append(LOWER_CASE)
    checklist.append(LOWER_CASE)
if incl_int == True:
    char_types.append(NUMBERS)
    checklist.append(NUMBERS)
if incl_sym == True:
    char_types.append(SYMBOLS)
    checklist.append(SYMBOLS)
while True:
    count = 0
    while count != length:
        case = char_types[random.randint(0,int(len(char_types)-1))]
        char = case[random.randint(0,int(len(case)-1))]
        password.append(str(char))
        count = count + 1
    # for every char in password, for every type in list, for every character in type, check if that is in password
    for i in range(len(password)):
        for x in range(len(checklist)):
            for y in range(len(checklist[x])):
                if checklist[x[y]] in password[i]:
                    checklist[x] = True
    for i in range(len(checklist)):
        if checklist[i] != True or checklist[i] != False:
            checklist[i] = False
    if all(checklist):
        random.shuffle(password) # added security?
        password = "".join(password)
        return password

我检查了变量、数据类型和可能的语法错误,但没有发现任何可疑之处。在

如果有人能提供一些关于这个问题是如何产生的想法,我们将不胜感激。我不经常在这里发问,所以如果这个问题看起来不完整,我道歉。在

谢谢你的帮助。在


Tags: 代码infalsetrueforinputifpassword
1条回答
网友
1楼 · 发布于 2024-05-19 01:13:25

在读取用户输入后,必须将length转换为整数。否则,您将比较一个整数和一个字符串,而字符串永远不会相等。所以你进入了无限循环。在

length = int(input('# input length of password in integers | '))

相关问题 更多 >

    热门问题