jeudi 21 juillet 2022

Select characters multiple times with python random

I started to learn python and wanted to do a small project. The project is quite simple. The script should be creating random password with the lenght of user's input. Here the code:

#IMPORTS
from datetime import datetime
import random

#VARIABLES
date = datetime.now()
dateFormat = str(date.strftime("%d-%m-%Y %H:%M:%S"))
lowerCase = "abcdefghijklmnopqrstuvwxyz"
upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
numbers = "0123456789"
symbols = "!?%&@#+*"
passwordConstructor = lowerCase + upperCase + numbers + symbols
userName = str(input("Enter username: "))
passwordLength = int(input("Enter the length of password: "))
f = open(userName.upper() + " - " + dateFormat + ".txt","w+") #generate txt-filename

#GENERATOR
password = "".join(random.sample(passwordConstructor, passwordLength))

#OUTPUT
print(userName + "'s generated password is: " + password)
f.write("USERNAME: " + userName + "\nPASSWORD: " + password + "\n\nGENERATED ON: " + dateFormat)
f.close()

But here the characters where choosed just once. But how do I get it done, so for example the letter "a" can be choosed multiple time.

Usecase: NOW I enter password length 7. The output would be: "abc1234" (of course random order)

EXPECTED I enter password length 10. The output should be: "aaabcc1221" (of course random order)

Thanks for the help!




Aucun commentaire:

Enregistrer un commentaire