Coders Packet

Python Generate OTP

By Nallamsetty Sri Ramya

OTP generator in Python program, It gives One-time Passwords(OTP) of any number of digits.

import math as m
import random as r
import string as s
n=int(input("ENTER N VALUE="))#4 digit or 6 digit otp number #4
def generateotp(n):
    otp=""
    d=s.digits#0123456789
    for k in range(n):#k=0
        otp=otp+d[m.floor(r.random()*10)]
    return otp
print("OTP=",generateotp(n))

 

One-time Passwords(OTP) is a password valid for only one login session or transaction on a digital device.
It is used in almost every service like internet banking etc. They are generally a combination of 4 or 6 numeric digits.

STEPS INCLUDED IN THIS PROJECT:

STEP1: First import modules like math, random, and string.

       import math as m
import random as r
import string as s

           
STEP2: Next, take input from the user and define a function.

         n=int(input("ENTER N VALUE="))#4 digit or 6 digit otp number #4
         def generateotp(n):


STEP3: do logic of give problem statement and return the result.

          otp=""
          d=s.digits#0123456789
          for k in range(n): #k=0 k=1 k=2 k=3
                otp=otp+d[m.floor(r.random()*10)]
                return otp

            
STEP4: Print the function in the output screen:

            print("OTP=",generateotp(n))

 

TAKE n=4 SO,

WE GET 4 RANDOM NUMBERS BETWEEN 0 TO 1:

#0.56 0.21 0.87 0.97

CASE 1: when k=0

#otp=0+d[m.floor(0.56*10)]
#otp=0+d[m.floor(5.6)]
#otp=0+d[5]
#otp=0+5
#otp=5


5   
-    -   -    -

CASE 2: when k=1


#otp=5+d[m.floor(0.21*10)]
#otp=5+d[m.floor(2.1)]
#otp=5+d[2]
#otp=5+2//strings
#otp=52


5   2   
-    -   -    -


CASE 3: when k=2

#otp=52+d[m.floor(0.87*10)]
#otp=52+d[m.floor(8.7)]
#otp=52+d[8]
#otp=52+8//strings
#otp=528

5   2   8   
-    -   -    -


CASE 4: when k=3

#otp=528+d[m.floor(0.97*10)]
#otp=528+d[m.floor(9.7)]
#otp=528+d[9]
#otp=528+9//strings
#otp=5289


5   2   8   9
-    -   -    -

CASE 5: WHEN K=4

         #BREAK

 

RESULT:The 4 digits otp is generated successfully.

Download Complete Code

Comments

No comments yet