0

Trying to change a list so i can generate a password

passwordlength=int(input('Enter how long you would like the password')) # The user input 

for x in range(0,passwordlength):         
       password=''.join(characters)                
       print(password)

That is what i am working with now. Characters is what i am using for my list. This is giving me for the input the list repeated for the input number.

Every time i try and use a append to the list i just end up going backwards

Any help would be appreciated

5
  • Could you explain what you expect password to be? eg - are you expecting a string of length passwordlength chosen randomnly from characters for instance? Commented Mar 20, 2015 at 22:28
  • what is in characters? Commented Mar 20, 2015 at 22:29
  • The list i have made is just a lot of numbers and letters , i am randomizing them every time. Commented Mar 20, 2015 at 22:30
  • Characters is a list of numbers and letters Commented Mar 20, 2015 at 22:30
  • you just keep assigning password to the joined elements of your list, what do you expect to happen? Maybe password=''.join(characters[:passwordlength]) is what you want Commented Mar 20, 2015 at 22:31

4 Answers 4

2

I think random.sample may be what you want:

from random import sample

passwordlength = int(input('Enter how long you would like the password')) # The user input 


password = ''.join(sample(characters,passwordlength))

Or else take a slice up to passwordlength:

password = ''.join(characters[:passwordlength]) 

To validate the user input we can use a try/except and a while loop:

from random import sample

while True:
    try:
        password_length = int(input('Enter password length between 1-{}'.format(len(characters)))) # The user input
        if password_length > len(characters):
            print("Password is too long")
            continue
        password = ' '.join(sample(characters,password_length))
        break
    except ValueError:
         print("Please enter digits only")

If you have ints in your character list you will need to map to str before joining.

password = ' '.join(map(str,sample(characters,password_length)))
Sign up to request clarification or add additional context in comments.

5 Comments

I like the first suggestion better.
@Padraic should probably do a check or a try/except around the sample for where the input'd length > len(characters)... For the second, to handle that case, then you can cycle the input before the slice
@JonClements, True, keeping me honest as always ;)
@Padriac... umm... too much with one except... I'd put the length check and error and a continue and let the ValueError handle the int failure instead...
@JonClements, yep, I think that was more for my own amusement.
0

Try it like this:

passwordlength=int(input('Enter how long you would like the password')) # The user input 
characters=['1','2','3','4','5','a','b','b']
password=[]
for x in range(0,passwordlength):         
       password.append(characters[x])                

print(''.join(password))

Comments

0

I'm sorry, but this question isn't completely clear.

Here are the assumptions that I am making here:

  • passwordlength is an integer containing the length to truncate to (which is in the code)
  • characters is a list of characters / numbers that you want to use for the password. e.g. characters = ['1', '2' ,'a' , 'c', 'd'] etc.

I don't understand what you are trying to do with the for loop?

I believe the following should do what you want.

password = ''.join(characters)[0:passwordlength]

3 Comments

Run the code and you'll see that either method i provide will give you a randomly generated password of length specified by the user.
The code in the original question does not generate a random password, At least not as written.
Fair enough, just supplying variation.
0

Here is some is a mock set up. Where choice will randomly choice an item from a list

from random import choice

myaplha = #somelist of letters to choice from
password = int(input("Enter password length")):

passw = []
for i in range(password):
    passw.append(choice(myaplha))
print("".join(passw))

or

password = "".join([choice(myaplha) for i in range(int(input("Enter password length")))])
print(password)

3 Comments

Okay... the latest edit is a fair attempt, but you're missing a . after the "", and range doesn't a str etc...
And the OP appears to be using print functions and int(input(...)) so you're missing a likely int to make sure range doesn't get a string
fixed, I assumed python2 which would cast a int string into an int if using input() instead of raw_input()