0

I have having trouble with my num_char output in my random password generator. I am getting the number of passwords but not the length on each password. Can you solve this?

from random import choice
import string

def create_password():

    num_passwords = int(input('How many passwords would you like?  '))
    num_char = int(input('How long would you like your password to be?  '))
    
    for j in range(num_passwords):
        for k in range(num_char):
            password = ''.join([choice(string.ascii_lowercase + string.ascii_uppercase + string.digits)])
            num_passwords -=1
        print(password)

create_password()

output:

y
t
K
S
7
1
  • 2
    Did you even try to use a debugger on this? Commented Jun 10, 2021 at 2:07

2 Answers 2

2

You might want to use list comprehension

Also, you don't need num_passwords -=1

And this

password = ''.join([choice(string.ascii_lowercase + string.ascii_uppercase + string.digits)])

Will give you something like a per password

from random import choice
import string

def create_password():

    num_passwords = int(input('How many passwords would you like?  '))
    num_char = int(input('How long would you like your password to be?  '))
    
    for j in range(num_passwords):
        pwd = [choice(string.ascii_lowercase + string.ascii_uppercase + string.digits) for k in range(num_char)]
        print(''.join(pwd))
create_password()

output (num_pwd = 5, num_char = 5)

VY1bi
jAvLG
zUxlo
jMBZD
VQHV6
Sign up to request clarification or add additional context in comments.

Comments

1

this maybe easier to understand for beginer.
but list comprehension is better solution

from random import choice
import string

def create_password():

    num_passwords = int(input('How many passwords would you like?  '))
    num_char = int(input('How long would you like your password to be?  '))
    
    for j in range(num_passwords):
        password = ''
        for k in range(num_char):
            #in your code.you just replace password to last one every time
            password += ''.join([choice(string.ascii_lowercase + string.ascii_uppercase + string.digits)])
            # num_passwords -=1 #no need for this
        print(password)

create_password()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.