I am fairly new to python and have been making a code that generates passwords. I have been trying to make it write all the way to the end of the line but for some reason it won’t write to the end.
The code is as follows:
Declarations:
import os, random, time
Colours = list(range(257))
Code Actual:
def Rand_Gen(Display, Colour):
alphabet = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
non_alphabet = ['1', '!', '2', '$', '3', '#', '4', '$', '5', '%', '6', '^', '7', '&', '8', '*', '9', '(', '0', ')', '-', '_', '=', '+', '[', '{', ']', '}', '|', ';', ':', ',', '<', '.', '>', '/', '?']
Choices = [alphabet, non_alphabet]
Inputted = []
max_num = 5
Combinate = ''
max_len = 7
# Truncating all files so that all items in that file are from this iteration:
# with open(r'D:\khohn15\Desktop\Python\Created_Files\Password_Generator\Time_Taken', 'w') as b:
# b.write('\n\nVersion: V3.2.F, Colour: ' + str(Colour) + ', Display: ' + str(Display) + ', Max length of generatables: ' + str(max_num) + '\n')
# with open(r'D:\khohn15\Desktop\Python\Created_Files\Password_Generator\Inputted', 'w') as NonAlphInput:
# NonAlphInput.write('List of Inputted Items:\n')
# Starting the loop process to generate passwords:
while True:
Av_Width = os.get_terminal_size().columns
things = []
time_I = time.time()
while Av_Width > max_num:
# Generating the item:
x = random.randrange(1, max_num)
for char in range(x):
List_Choice = random.choice(Choices)
Char_Choice = random.choice(List_Choice)
Combinate += Char_Choice
# Determining if the item has already been generated
if Combinate not in Inputted:
Tries = 0
Inputted.append(Combinate)
things.append(Combinate)
# with open(r'D:\khohn15\Desktop\Python\Created_Files\Password_Generator\Inputted', 'a') as NonAlphInput:
# NonAlphInput.write(' ' + Combinate + '\n')
else:
Tries += 1
Av_Width -= max_num + 1
Combinate = ''
# The display process, self explanatory.
if Display == True:
if Colour:
for i in things:
if things.index(i) == len(things)-1:
print('\033[38;5;' + str(random.choice(Colours)) + 'm' + '\033[48;5;' + str(random.choice(Colours)) + 'm' + i + ' ' + ' ' * (max_num - len(i)), end='')
else:
print('\033[48;5;' + str(random.choice(Colours)) + 'm' + '\033[48;5;' + str(random.choice(Colours)) + 'm' + i + ' ' + ' ' * (max_num - len(i)), end='')
print()
else:
for i in things:
if things.index(i) == len(things)-1:
print(i + ' ' + ' ' * (max_num - len(i)+1), end='')
else:
print(i + ' ' + ' ' * (max_num - len(i)), end='')
print()
time_II = time.time()
time_taken = time_II - time_I
time_taken = '{:.3f}'.format(time_taken)
# with open(r'D:\khohn15\Desktop\Python\Created_Files\Password_Generator\Time_Taken', 'a') as b:
# b.write(' Iteration ' + str(reps) + ' took ' + str(time_taken) + ' seconds.\n')
if Tries == 10000000000000000000000000000:
break
Inputted.sort()
for i in Inputted:
# with open(r'D:\khohn15\Desktop\Python\Created_Files\Password_Generator\Inputted_Alphabetized', 'a') as Input:
# Input.write(i + '\n')
print(Inputted)
To initiate, just type:
Rand_Gen(True, True)
If you want colour set the last item as True, if no colour set it to false. But to see what I am talking about make sure the second last item is True.
The code that I believe is causing the issue is:
Av_Width = os.get_terminal_size().columns
# and
while Av_Width > max_num:
Here is the code that actually shows the error:
def Eg():
alphabet = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
non_alphabet = ['1', '!', '2', '$', '3', '#', '4', '$', '5', '%', '6', '^', '7', '&', '8', '*', '9', '(', '0', ')', '-', '_', '=', '+', '[', '{', ']', '}', '|', ';', ':', ',', '<', '.', '>', '/', '?']
Choices = [alphabet, non_alphabet]
Inputted = []
max_num = 5
Combinate = ''
while True:
reps += 1
Av_Width = os.get_terminal_size().columns
things = []
time_I = time.time()
while Av_Width > max_num:
# Generating the item:
x = random.randrange(1, max_num)
for char in range(x):
List_Choice = random.choice(Choices)
Char_Choice = random.choice(List_Choice)
Combinate += Char_Choice
# Determining if the item has already been generated
if Combinate not in Inputted:
Tries = 0
Inputted.append(Combinate)
things.append(Combinate)
# with open(r'D:\khohn15\Desktop\Python\Created_Files\Password_Generator\Inputted', 'a') as NonAlphInput:
# NonAlphInput.write(' ' + Combinate + '\n')
else:
Tries += 1
Av_Width -= max_num + 1
Combinate = ''
for i in things:
print(i + ' ' + ' ' * (max_num - len(i)+1), end='')
Eg()
Can anyone show what needs to change so that it writes to the end, and I will take any general practice advice.