Skip to main content
edited tags
Link
Reinderien
  • 71.2k
  • 5
  • 76
  • 257
Tweeted twitter.com/StackCodeReview/status/1304434546910343177
added 522 characters in body
Source Link
Mast
  • 13.9k
  • 12
  • 57
  • 128
import random
def password_generator():
    password = []
    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"]
    password_length = 0
    password_numbers = []
    password_letters = []
    # Input the length of the password
    while True:
        password_length_input = input("Choose the length of your password with numbers between 6 and 15:\n")
        if not password_length_input.isnumeric():
            print(f"{password_length_input} is not a number, try again:")
            continue
        else:
            password_length = int(password_length_input)
            print(f"Password length: {password_length}")
        if 6 <= password_length <= 15:
            break
        else:
            print("The password must be between 6 and 15 characters, try again:")
            continue
    # Input the amount of numbers in password
    while True:
        password_numbers_input = \
            input(f"Choose the amount of numbers you want in your password, max {password_length}\n")
        if not password_numbers_input.isnumeric():
            print(f"{password_numbers_input} is not a number try again")
            continue
        elif int(password_numbers_input) > password_length:
            password_numbers = 0
            print(f"The value is too high, choose maximum {password_length} numbers")
            continue
        else:
            password_numbers = int(password_numbers_input)
            print(f"Password numbers: {password_numbers}")
            for number in range(0,password_numbers):
                password.append(random.randrange(0,9))
            break
    # Check for numbers and letters in password
    while True:
        if password_numbers == password_length:
            print(f"The password will be only {password_numbers} numbers, no letters.")
            break
        else:
            password_letters = password_length - password_numbers
            print(f"""Your password will be {password_length} characters with {password_numbers} numbers and {password_letters} letters.""")
            for letter in range(0,password_letters):
                password.append(random.choice(letters))
            break
    random.shuffle(password)
    password_string = ''.join([str(item) for item in password])
    print(f"Your password is:\n{password_string}")

password_generator()

password_generator()Usage example:

Choose the length of your password with numbers between 6 and 15:
Password length: 8
Choose the amount of numbers you want in your password, max 8
Password numbers: 2
Your password will be 8 characters with 2 numbers and 6 letters.
Your password is:
pzc11bmf
import random
def password_generator():
password = []
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"]
password_length = 0
password_numbers = []
password_letters = []
# Input the length of the password
while True:
    password_length_input = input("Choose the length of your password with numbers between 6 and 15:\n")
    if not password_length_input.isnumeric():
        print(f"{password_length_input} is not a number, try again:")
        continue
    else:
        password_length = int(password_length_input)
        print(f"Password length: {password_length}")
    if 6 <= password_length <= 15:
        break
    else:
        print("The password must be between 6 and 15 characters, try again:")
        continue
# Input the amount of numbers in password
while True:
    password_numbers_input = \
        input(f"Choose the amount of numbers you want in your password, max {password_length}\n")
    if not password_numbers_input.isnumeric():
        print(f"{password_numbers_input} is not a number try again")
        continue
    elif int(password_numbers_input) > password_length:
        password_numbers = 0
        print(f"The value is too high, choose maximum {password_length} numbers")
        continue
    else:
        password_numbers = int(password_numbers_input)
        print(f"Password numbers: {password_numbers}")
        for number in range(0,password_numbers):
            password.append(random.randrange(0,9))
        break
# Check for numbers and letters in password
while True:
    if password_numbers == password_length:
        print(f"The password will be only {password_numbers} numbers, no letters.")
        break
    else:
        password_letters = password_length - password_numbers
        print(f"""Your password will be {password_length} characters with {password_numbers} numbers and {password_letters} letters.""")
        for letter in range(0,password_letters):
            password.append(random.choice(letters))
        break
random.shuffle(password)
password_string = ''.join([str(item) for item in password])
print(f"Your password is:\n{password_string}")

password_generator()

import random
def password_generator():
    password = []
    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"]
    password_length = 0
    password_numbers = []
    password_letters = []
    # Input the length of the password
    while True:
        password_length_input = input("Choose the length of your password with numbers between 6 and 15:\n")
        if not password_length_input.isnumeric():
            print(f"{password_length_input} is not a number, try again:")
            continue
        else:
            password_length = int(password_length_input)
            print(f"Password length: {password_length}")
        if 6 <= password_length <= 15:
            break
        else:
            print("The password must be between 6 and 15 characters, try again:")
            continue
    # Input the amount of numbers in password
    while True:
        password_numbers_input = \
            input(f"Choose the amount of numbers you want in your password, max {password_length}\n")
        if not password_numbers_input.isnumeric():
            print(f"{password_numbers_input} is not a number try again")
            continue
        elif int(password_numbers_input) > password_length:
            password_numbers = 0
            print(f"The value is too high, choose maximum {password_length} numbers")
            continue
        else:
            password_numbers = int(password_numbers_input)
            print(f"Password numbers: {password_numbers}")
            for number in range(0,password_numbers):
                password.append(random.randrange(0,9))
            break
    # Check for numbers and letters in password
    while True:
        if password_numbers == password_length:
            print(f"The password will be only {password_numbers} numbers, no letters.")
            break
        else:
            password_letters = password_length - password_numbers
            print(f"""Your password will be {password_length} characters with {password_numbers} numbers and {password_letters} letters.""")
            for letter in range(0,password_letters):
                password.append(random.choice(letters))
            break
    random.shuffle(password)
    password_string = ''.join([str(item) for item in password])
    print(f"Your password is:\n{password_string}")

password_generator()

Usage example:

Choose the length of your password with numbers between 6 and 15:
Password length: 8
Choose the amount of numbers you want in your password, max 8
Password numbers: 2
Your password will be 8 characters with 2 numbers and 6 letters.
Your password is:
pzc11bmf
Became Hot Network Question
edited tags
Source Link
Peilonrayz
  • 44.6k
  • 7
  • 80
  • 158

Improving a Password Generator First password generator in Python 3

This is my first project inusing Python, this is. I made a simple password generator with checking forthat checks user inputs, wondering on how toinput. How can I improve it. Thank you?

Improving a Password Generator in Python 3

This is my first project in Python, this is a simple password generator with checking for user inputs, wondering on how to improve it. Thank you

First password generator in Python

This is my first project using Python. I made a simple password generator that checks user input. How can I improve it?

edited title
Link
Loading
Source Link
Loading