Issue
Edit (23 June 2023): tl;dr I forgot to convert data types.
I was initially prevented from deleting this question and have now decided against it. This remains as a reminder to myself and others to thoroughly research questions.
System: 64-bit Windows 10 machine
Python installation: Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32
Editor: original code from sublime text 3, now editing on VS code [latest stable build]
I am quite new to python, and have been working on a password manager program run in cmd.
I have used a function I made previously to generate random strings[passwords] to be stored in a file by main script. It is imported into main script from another py file. the code hangs on the second line of code below in the while count != length loop[code from the imported script] the exact same line of code had worked before sorry the sample code is way too lengthy.
length: a variable defined by user input; turned into int to be used later
count: a variale is set to 0 before this loop
char_types: some lists of characters[upper case, lower case, symbols, numbers]
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
I have checked through the variables, data types and possible syntax errors, only to find nothing of suspicion.
It would be very much appreciated if anyone could provide some ideas as to how this problem is produced. I don't often post questions much here, so I apologise if this question seems incomplete.
Thanks for your help.
Solution
You have to convert length
to an integer after reading the user input. Otherwise you are comparing an integer with a string, which is never going to be equal. Hence you get in infinite loop.
length = int(input('# input length of password in integers | '))
Answered By - rettenda
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.