Items not displaying to the end of line

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.

You have not included all the code that someone can test.

You are refering to files that only exist on your computer.

The function Rand_Gen is shown with 2 arguments,
but your example calls with 4 arguments.

What do you mean by display to end of line?

1 Like

To be fair, those files that exist only on his PC are also created in the script.

But it’s certainly true that Rand_Gen(alphabet, non_alphabet, True, True) can’t run with the provided code.
I think it’s also reasonable to expect you to find which chunk of lines isn’t working. This is a lot of code that’s somewhat unpleasant to read. If you can narrow it down to 5 self-contained lines that don’t do what you expect them to do, I’d be more willing to help.
At that point you might also be able to solve it yourself :slight_smile:

1 Like

Fixed, sorry I didn’t look over closely enough.

Fixed. I commented out the code that creates files as it is not required in the code for the issue.

ok, I see you tried

It still won’t run at all for me.
I had to add reps=0 to avoid an error, so you can’t have actually run this code yourself?
It then ran for 22 seconds without doing anything, so I killed the process. My IDE tells me the while loop is unbreakable, so you can’t actually have run this code yourself?

Now getting back to those 5-line chunks:
What I meant with “find a chunk of 5 lines that doesn’t do what you expect” is something like:

You test the chunk

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

if it works as expected, great. Put it in a function

def whatever(max_num, Choices):
  "returns for example 'XBOP?'"
  for char in range(x):
    List_Choice = random.choice(Choices)
    Char_Choice = random.choice(List_Choice)
    Combinate += Char_Choice
    return Combinate

and put that function in your code so it’s easier to read.

Then actually just replace the function with a constant that represents something the function might have returned.

Continue with

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 = ''

if that doesn’t do what you expect it to do, break it down further. Test the individual expressions. (This is easy to do in for example Spyder. You can also sprinkle print statements throughout your code.)

Eventually you have a loop with just 4 lines in it instead of

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 = ''

and most of those lines are completely trivial.

So you reduce your problem to a core statement that doesn’t do what you think it does.

1 Like

Ok. For some reason it isn’t allowing me to to edit the post but here is a simplified example:

import os, random, time

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']
    Inputted = []
    max_num = 5
    Combinate = ''
    while True:
        Av_Width = os.get_terminal_size().columns
        things = []
        while Av_Width > max_num:
# Generating the item:
            x = random.randrange(1, max_num)
            for char in range(x):
                Char_Choice = random.choice(alphabet)
                Combinate += Char_Choice
            Tries = 0
            things.append(Combinate)
            Av_Width -= max_num + 1
            Combinate = ''
        for i in things:
            print(i + ' ' + ' ' * (max_num - len(i)+1), end='')
        time.sleep(0.25)

Eg()