Why is the output getting longer

I am making a password generating program.It should print out 5 passwords basewd on the number of characters the user wants the passwords to have. Its source code is below:

from tkinter import *
from tkinter.ttk import * 
import random
password = ""
class password_creator(): 
    def __init__(self,n,jumbled_output,final_output): #making the local variable
        self.n  = n
        self.jumbled_output = jumbled_output
        self.final_output = final_output
    def alpha_rand_func(self): # for alphabetical part of the password
        alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
        alpha_pass = "" # stores alphabetical part of the variable
        for i in range(0,(self.n//2)): #for inserting random alphabets
            alpha_pass +=  random.choice(alpha)
        for x in range(self.n // 2):
              self.jumbled_output += random.choice(alpha_pass) #for inserting random alphabets shortlisted previously in jumbled_output
        self.n -= self.n // 2
        #print(alpha_pass)
        alpha_pass = ""
    def int_rand_func(self):  #for numerical part of the password
        int_ = str(1234567890) 
        int_pass = "" #stores numerical part of the variable
        for i in range(0,self.n):       
            int_pass  += random.choice(int_) #inserting random integers for the
        for x in range(self.n): #for inserting random numbers shortlisted previously in jumbled_output
            self.jumbled_output += random.choice(int_pass)        
        #print(int_pass)
        int_pass = ""
    def symbol_rand_func(self):
        symbol = ",./;'\[]-=`<>?:|{ }_+~" 
        sym_pass = "" #for storing symbolic part of the password
        for i in range(0,5):
            sym_pass += random.choice(symbol) #inserting random symbols in sym_pass
        for i in range(5):
            self.jumbled_output += random.choice(sym_pass) #for inserting random symbols shortlisted previously in jumbled_output
        self.n -= 5

        #print(sym_pass)
        sym_pass = ""
    def jumbling_func(self):
       global password
       for i in self.jumbled_output:
           self.final_output += random.choice(self.jumbled_output)
       password += self.final_output
       
    def clear(self): # for clearing the final_output ,password, jumbled_output
        self.final_output = ""
        self.jumbled_output = ""
        password = ""


no_of_char = int(input("no. of characters: ")) # user input for no. of characters in the password generated
inp_ = password_creator(no_of_char,"","") 
def executioner(): #for calling the methods
 inp_.symbol_rand_func()
 inp_.alpha_rand_func()
 inp_.int_rand_func()
 inp_.jumbling_func()
 print(password)
 inp_.clear()
 return " "
for x in range(5):
   print(executioner()) # calling the above function

The output I am receiving is

no. of password: 10
e|-|e|8NNN
 
e|-|e|8NNN[[{[[
 
e|-|e|8NNN[[{[[[,[,[
 
e|-|e|8NNN[[{[[[,[,[ >> }
 
e|-|e|8NNN[[{[[[,[,[ >> }]]+:+

Now, this is not the output that I expect to receive. The output should contain strings of same size.
Therefore, I would ask the community to help me in resolving this issue.

I don’t know the language well but I imaging that if you define password outside the function it will grow with each execution

1 Like

I would rework this to have a getter for the generated password stored inside the object.

Also, you can significantly reduce the amount of code that you have if you refractor. The same code is replicated in each *_rand_func, which should be a tip off to refactor.

Also, as a general warning, random is not truly random and should not be relied upon for cryptographic purposes.

1 Like