Search code examples
pythonpython-3.xrandomsamplepassword-generator

I'm trying to make a password generetor using Python 3.7. I searched online how to use sample() but for me it doesn't work


So i want it to pick a random digit from a list 2 times nd then mix it together in order to get a password. I wanted the ready_password to print a list and it works but, it also prints [] and ''. So i decided to make it a tuple but i don't know how to mix a tuple. This is the error that I'm getting:

TypeError: sample() missing 1 required positional argument: 'k'

code-

import random

lower_case = ['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']

upper_case = ['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 = ['1', '2', '3', '4', '5', '6', '7', '8', '9']

punctuation = ['!', '@', '#', '$', '%', '^', '&', '*', '(']

lower1 = random.choice(lower_case)
lower2 = random.choice(lower_case)

upper1 = random.choice(upper_case)
upper2 = random.choice(upper_case)

number1 = random.choice(numbers)
number2 = random.choice(numbers)

punctuation1 = random.choice(punctuation)
punctuation2 = random.choice(punctuation)

password_not_ready = (lower1 + lower2 + upper1 + upper2 + number1 + number2 + punctuation1 + 
punctuation2)

ready_password = random.sample(password_not_ready)

print(ready_password)
   

Solution

  • You might want to use random.shuffle() to mix up the characters instead of having them in order:

    from random import sample as sp
    from random import shuffle as sh
    
    lower_case = 'abcdefghijklmnopqrstuvwxyz'
    
    upper_case = lower_case.upper()
    
    numbers = '1234567890'
    
    punctuation = '!@#$%^&*('
    
    ready_password = sp(lower_case,2)+sp(upper_case,2)+sp(punctuation,2)+sp(numbers,2)
    
    sh(ready_password)
    
    print(''.join(ready_password))
    

    Output:

    c3D#0hV%