Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm writing a code for Random Password generator where user should have an ability to select the length of password from pre-defined range (15,25) and code will generate random password using letters, numbers and symbols. If a user enters a password length that is out of range, they should get a message as 'Try again and select number between pre-defined range of 15-25'.

The issue I'm experiencing is that user password length is 3x the user input.Ex: If the user enter the password length as 15, random password generator is creating a 45 char password.

What am I doing wrong?

What I have tried:

Here's code:
Python
import random
letters = ['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']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']

password_generator = int(input("Please enter your desired password length between (15 - 25)\n"))

if 15 <= int(password_generator) <= 25:
password_list = []
for char in range(1, password_generator+1):
    random_char = random.choice(letters) + random.choice(numbers) + random.choice(symbols) 
    password_list += random_char 

password = ""

for char in password_list:
    password += char
print(password)
else:
password_generator = input("Try Again, Please select range between 15 and 25 range
Posted
Updated 24-May-22 3:45am
v2

Look at the line
Python
random_char = random.choice(letters) + random.choice(numbers) + random.choice(symbols)
For every iteration of the loop you are adding three characters to the array.

You could change your loop to be something like
Python
for char in range(1, int(password_generator / 3) + 1):
or not separate letters, numbers and symbols into separate arrays, just use one

Edit after OP comment: E.g. just one array
Python
characters = ['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', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9','!', '#', '$', '%', '&', '(', ')', '*', '+']
The the line that assigns a random character to the password array becomes
Python
random_char = random.choice(characters)
As an aside, is there a particular reason why you put the value into an array and not just append it directly to the variable password? Saves a loop. Also - take care with your indentation
 
Share this answer
 
v2
Comments
Manbir Singh 2022 24-May-22 9:45am    
Thanks.. It worked with updating the loop.

Just curious though as how can I use one array instead of separate ones that can cover numbers, letters and symbols?
CHill60 24-May-22 9:57am    
I've added some information to my solution
CHill60 24-May-22 9:58am    
I see that Solution 3 gives you another option
You need to select either letters or numbers or symbols each time, not all three. Here is a sample that will do that automatically:
Python
import random
letters = ['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']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
# create a list of the three types
lns = [letters, numbers, symbols ]

password_generator = int(input("Please enter your desired password length between (15 - 25)\n"))

if 15 <= int(password_generator) <= 25:
  password = ""
  random.seed()

  for i in range(password_generator):
    # get an index of the three types
    type = random.randrange(3)
    # now select a random character from the selected type
    random_char = random.choice(lns[type]) 
    password += random_char 
  print(password)
else:
  # this only needs to be a message
  print("Please try again and select range between 15 and 25")
 
Share this answer
 
Comments
Manbir Singh 2022 24-May-22 10:06am    
I like this optimized code option!

Thanks
Richard MacCutchan 24-May-22 10:16am    
You are welcome.
The way I do it is simple - just use a GUID: uuid — UUID objects according to RFC 4122 — Python 3.10.4 documentation[^]
Create it as a Hex string and you get something like this: 11069f3e-bc2b-469c-ab00-2297d1c7e07f

Use that as a password and it does two things:
1) Provides a strong password!
2) Encourages the user to change it to something he can remember as quickly as possible ...

But it's worth noting that password strength is an odd thing: technically strong passwords are often much weaker than technically weak passwords in the real world as they have to be written down instead of remembered. Have a look here: NCSC: The logic behind three random words[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900