DEV Community

Vaarun Sinha
Vaarun Sinha

Posted on

How to make a password generator with python.

In this tutorial we will make a password generator with the secrets module. Why not random module? see the first post in this series to understand.

So Let's Begin!

Problem

  • Generate a random password, according to the user input.

Sub problems

  • Get user input
  • Create lists for letters etc...
  • Loop over lists and make a password.
  • Print the password.

Let's write some code!

  • Get user input Here we will use try/except blocks so that when user types a string we do not get an error.

We will get inputs for number of special characters, small letters, big letters and numbers required for generating the password.

Code:

try:
        num_of_numbers = int(input("How many numbers should your password have: "))
except ValueError:
        num_of_numbers = int(input("How many numbers should your password have: "))
try:
        num_of_special = int(
        input("How many special characters should your password have: "))
except ValueError:
        num_of_special = int(
        input("How many special characters should your password have: "))
try:
        num_of_small_letters = int(
        input("How many small letters should your password have: "))
except ValueError:
        num_of_small_letters = int(
        input("How many small letters should your password have: "))
try:
        num_of_cap_letters = int(
        input("How many big letters should your password have:  "))
except ValueError:
        num_of_cap_letters = int(
        input("How many big letters should your password have:  "))

Enter fullscreen mode Exit fullscreen mode

### Create Lists for numbers to choose from.

  • We could hardcode this like:
number_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Enter fullscreen mode Exit fullscreen mode
  • But this will take us way more time!

  • So let's use the string module!

import string
number_list = list(string.digits)
Enter fullscreen mode Exit fullscreen mode

But we could just have a string right? then why make it list you will find this out later on in this blog!

Let's do this for everything.

import string

smallLetters = list(string.ascii_lowercase)

bigLetters = list(string.ascii_uppercase)

specialCharacters = list(string.punctuation)

numbers = list(string.digits)

Enter fullscreen mode Exit fullscreen mode

So now we can loop over these lists, and make a list!

First let's declare empty variables!

spPart = ""

numPart = ""

smallPart = ""

bigPart = ""
Enter fullscreen mode Exit fullscreen mode

Now let's loop over the lists.

    for i in range(1, num_of_numbers + 1):
        randNum = secrets.choice(numbers)
        numPart = numPart + str(randNum)


    for i in range(1, num_of_special + 1):
        randSp = secrets.choice(specialCharacters)
        spPart = spPart + randSp


    for i in range(1, num_of_small_letters + 1):
        randSm = secrets.choice(smallLetters)
        smallPart = smallPart + randSm


    for i in range(1, num_of_cap_letters + 1):
        randBig = secrets.choice(bigLetters)
        bigPart = bigPart + randBig
Enter fullscreen mode Exit fullscreen mode

Now let's add the parts to make a password.

password = numPart + spPart + smallPart + bigPart
Enter fullscreen mode Exit fullscreen mode

Now let's refactor the code, by putting this into a function.


def generate_password( num_of_numbers, num_of_special, num_of_small_letters, num_of_cap_letters):

    smallLetters = list(string.ascii_lowercase)

    bigLetters = list(string.ascii_uppercase)

    specialCharacters = list(string.punctuation)

    numbers = list(string.digits)

    spPart = ""

    numPart = ""

    smallPart = ""

    bigPart = ""

    # ANCHOR: Generating Password
    for i in range(1, num_of_numbers + 1):
        randNum = secrets.choice(numbers)
        numPart = numPart + str(randNum)


    for i in range(1, num_of_special + 1):
        randSp = secrets.choice(specialCharacters)
        spPart = spPart + randSp


    for i in range(1, num_of_small_letters + 1):
        randSm = secrets.choice(smallLetters)
        smallPart = smallPart + randSm


    for i in range(1, num_of_cap_letters + 1):
        randBig = secrets.choice(bigLetters)
        bigPart = bigPart + randBig

    password = numPart + spPart + smallPart + bigPart

    return password
Enter fullscreen mode Exit fullscreen mode

Now if we print/generate the password every time ,there is a predictable format, first numbers then special etc..

So how we can solve this?

We will solve this in the next short blog coming very quickly after this blog, so stay tuned!

Happy Coding!

Top comments (1)

Collapse
 
vaarun_sinha profile image
Vaarun Sinha • Edited

Feedback is highly appreciated, if you find any problem/mistakes (which is unlikely) then please comment the problem and I will fix it.

Hope you learnt something valuable today.

Hope You Have A Nice Day Ahead!

The another blog is coming by 2 days

Edit: The blog has been released!
Happy Coding!