Create Random Password Generator (OTP) using Python | Python Projects for Beginners

Hello and welcome,

we are going to develop a simple python application known as Random Password Generator also known as OTP generator using a simple python inbuilt module named random.

In our previous articles, we have used random modules in developing a small project using the random module as Snake - Water and Gun Game. Please look at that project too which will help you to develop a basic understanding of the random module that will help in this project.

Table of Contents

  1. Brief Overview about Task
  2. Hands-on Implementation of Project from scratch
  3. Project Output Demo
  4. Concluding the Project
  5. Simple Practice Exercise for you.          

Random Password Generator project using python
Random Password Generator in Python

let's get started with this activity. First, it's essential to understand the task what do actually have to do?

Understanding the task

we have to display a password of the desired length specified by the user. Input the number of uppercase, lowercase, digits, and special character the user requires in the password. After taking all these inputs handle some common exceptions which may occur to select the random desired numbers from the ASCII list. Join the obtained characters and digits shuffle them to look like a One-time password and display it on screen.

✌ Now, we have understood the problem statement so let's make our hands dirty by writing some code in a pythonic way.

Implementation

Step-1) Import a random module first and take specified Inputs from the user.

import random

#take inputs from a user.
while True: 
    num_char = int(input("enter total char you want in password"))
    num_upper = int(input("total num of upperCase you want"))
    num_lower = int(input("total num of lowerCase you want"))
    num_digit = int(input("total num of digits you want"))      
    num_symbol = int(input("total num of special char you want"))

we have taken the inputs under a while loop to handle one exception that is if the desired number of characters is more than the required total length of the password given by the user so we will raise an error if such a case occurs. So, the below code continues in a while loop.
    if num_char < num_upper + num_lower + num_digit + num_symbol:
        print("Total char does not match your demand!\n") 
    else:    
        break

If the inputs are correct, we will come out of the loop. And if num_char is greater than the desired number of inputs then an error will not be there because we will randomly generate the char if remaining to fulfill the requested length of the password. 


Step-2) Take the list of requested characters from the ASCII list.

The below code will be outside the While loop because we got the correct inputs from a user and now we have to create a list from where we will select the required random of specific characters.

upper_list = [chr(i) for i in range(65, 65+26)]  
lower_list = [chr(i) for i in range(95, 95+26)]  
digit_list = [str(i) for i in range(0, 10)]      
symbol_list = [chr(i) for i in range(32, 48)]    
symbol_list += [chr(i) for i in range(58, 65)]   
symbol_list += [chr(i) for i in range(91, 97)]   
symbol_list += [chr(i) for i in range(123, 127)] 

Here, we have taken the list of upper, lower digits, and special character lists. special characters are present at a different location so we have to grab them in a list using the append operator or you can also use the append function of the list.


Step-3) Randomly chooses the required characters from a list.

Implement a function where we will be passing the ASCII list which we created and the number of particular characters we required and get the randomly chosen characters from the list in return in form of a list.

def getChar(char_list, number): 
    temp_list = []    
    for i in range(number):  
        temp_list.append(random.choice(char_list) 
    return temp_list

Step-4) Call the function and get the requested characters randomly

We have created a function that accept the list and required random character to choose from. So now we will call the function for the corresponding list and accept the required number of particular characters in the password.

upper_char = getChar(upper_list, num_upper) 
lower_char = getChar(lower_list, num_lower) 
digit_char = getChar(digit_list, num_digit)      
symbol_char = getChar(symbol_list, symbol_upper) 

Step-5) Control the exception of remaining characters

If the user has inputted fewer characters and the length of a password is more, then we have to fill in the remaining character to generate a password of the requested length.

num_unfilled_char = num_char - num_upper - num_lower - num_digit - num_symbol 
whole_list = upper_list + lower_list + digit_list + symbol_list   
remaining_char = getChar(whole_list, num_unfilled_char)

Step-6) Join and shuffle the Password.

Now, we have all the types of required characters for passwords with us, only we have to combine and shuffle them randomly to generate the OTP. So let's implement the last step fast.

password = upper_char + lower_char + digit_char + symbol_char + remaining_char  
random.shuffle(password)        
password = "".join(password)   
print("Your OTP: ", password)
Congratulations! we have made our application. if you run it OTP will be generated on your screen.

OUTPUT Window for Project Demo

when you run the code you will have the output generated like this.

Password (OTP) Generator using Python demo

The complete code you can find at my GitHub: Password Generator in Python

NOTE: use the name of the variable relevant which you are capable to understand. the name of a variable in this code can seem big but it is easy to understand. when you will work on a big codebase, then it will be easy to remember on which variable what constraint you have applied.

Conclusion

We are capable to understand and use a random module in a very nice way. We have implemented 2 small projects using a random module. I hope you are now able to use it to solve problem statements related to it and enjoyed the complete tutorial. I have an exercise to implement a number-guessing game using a random module between user and system. And paste the code or upload code at GitHub and paste the link in the comment section below. I will provide a solution to the same in the next article.

Simple Exercise for you

Build a random guessing number game between ranges and guide a user to guess it properly. display the number of trials he used. Upload your code on GitHub and paste the link in the comment section below.

keep Learning, Happy Learning

Complete Code in window


4 Comments

If you have any doubt or suggestions then, please let me know.

Previous Post Next Post