pythonpython-3.xrandompassword-manager

input isn't choosing the passwort length


I'm new to programming, and I'm making a password manager. My problem: -what it does -> when I run the program and enter the length of the password it generates a password, but it is always 4 digits long and the password gets repeated so many times as the digit I had put in. -What it should do -> the digit that I put in should determine the length of the password and not how many times it gets repeated.

import random

#shuffle the list
def shuffle(string):
  tempList = list(string)
  random.shuffle(tempList)
  return ''.join(tempList)

#the password functions
uppercaseLetter=chr(random.randint(65,90))
lowercaseLetter=chr(random.randint(97,122))
punctuationSign=chr(random.randint(32,152))
digit=chr(random.randint(48,57))

#completing the password
passwordLength = int(input("choose your password length: "))

possibleChars = uppercaseLetter, lowercaseLetter, punctuationSign, digit
ranChar = shuffle(possibleChars)

tempPassword = []

count = 0
while count != passwordLength:
    tempPassword.extend(ranChar)
    count = count + 1

password = ''.join(tempPassword)

#for which sitename
sitename = input('Save under which name: ')


print (password, sitename)

data=open("test.txt",'a')
data.write(sitename +'  ')
data.write(password +'\n')
data.close()

Solution

  • I think you are really over complicating what you need to do, here is what you're trying to do to the best of my knowledge

    import random
    
    #completing the password
    passwordLength = int(input("choose your password length: "))
    
    temp_pass = ''
    
    count = 0
    while count != passwordLength:
        char_type = random.randint(0,3)
        if char_type == 0:
            random_char = chr(random.randint(65,90))
        elif char_type == 1:
            random_char = chr(random.randint(97,122))
        elif char_type == 2:
            random_char = chr(random.randint(32,152))
        else:
            random_char = chr(random.randint(48,57))
        temp_pass = temp_pass + random_char
        count = count + 1
    
    
    print(temp_pass)
    

    Hope this helps. I would advise practicing the basics more before trying things like this, there is a lot of bad practises in the code.