I have written / modified a random password generator, and I need help with 1 part .
The part I need help with is:
if the user presses enter (leaves it blank) for the length of the password
then it would go to default value of 20
otherwise what ever the user enters. eg 10
it would need to be an integer for the rest of the program to work properly.
any help would be appreciated.
this is the code below for python3
The part I need help with is:
if the user presses enter (leaves it blank) for the length of the password
then it would go to default value of 20
otherwise what ever the user enters. eg 10
it would need to be an integer for the rest of the program to work properly.
any help would be appreciated.
this is the code below for python3
import string import random ## characters to generate password from alphabets = list(string.ascii_letters) digits = list(string.digits) special_characters = list("!@#$%^&*()") characters = list(string.ascii_letters + string.digits + "!@#$%^&*()") def random_password(): ## length of password from the user print('Default password length is 20') length = int(input("Enter password length or press enter for default password length: ")) length = sum([length,1]) print((length)-1) ## number of character types alphabets_count = int(input("Enter alphabets count in password: ")) while alphabets_count > length: print('Alphabets count is greater than password length') alphabets_count = int(input("Enter alphabets count in password: ")) else: dlength = (length - alphabets_count -1) digits_count = int(input("Enter digits count in password: ")) while digits_count > dlength: print('Digits Count is greater than password length') print('Enter ' + str(dlength) + ' digits or less') digits_count = int(input("Enter digits count in password: ")) else: sclength = (length - alphabets_count - digits_count -1) sclengthcalc = (length - alphabets_count - digits_count) special_characters_count = int(input("Enter special characters count in password: ")) while special_characters_count > sclengthcalc: print('Special characters Count is greater than password length') print('Enter ' + str(sclength) + ' special characters or less') special_characters_count = int(input("Enter special characters count in password: ")) else: pass pass characters_count = alphabets_count + digits_count + special_characters_count # initializing the password password = [] # picking random alphabets for i in range(alphabets_count): password.append(random.choice(alphabets)) ## picking random digits for i in range(digits_count): password.append(random.choice(digits)) ## picking random alphabets for i in range(special_characters_count): password.append(random.choice(special_characters)) # if the total characters count is less than the password length # add random characters to make it equal to the length if characters_count < length: random.shuffle(characters) for i in range(length - characters_count -1): password.append(random.choice(characters)) ## shuffling the resultant password random.shuffle(password) ### converting the list to string ### printing the list print("".join(password)) ### invoking the function random_password()
Larz60+ write Jan-17-2022, 07:08 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
fixed for you this time. please use bbcode tags on future posts.
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
fixed for you this time. please use bbcode tags on future posts.