Home > Software design >  How to store password in variable?
How to store password in variable?

Time:10-28

I have a function that generates a new password everytime I run the program. I want to know if its possible to store the generated password in a variable or function. How can i do this? code is below:

def generateOTP():
    # Declare a string variable
    # which stores all string
    string = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    OTP = ""
    length = len(string)
    for i in range(8):
        OTP  = string[math.floor(random.random() * length)]

    return OTP

print(generateOTP())

I'm not sure how to go around this. Any help would be great!

Thanks

CodePudding user response:

  1. bro it's already in you OTP var
import math, random
def generateOTP():
    # Declare a string variable
    # which stores all string
    string = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    OTP = ""
    length = len(string)
    for i in range(8):
        OTP  = string[math.floor(random.random() * length)]

    return OTP


otp = generateOTP()

print("here you go: ",otp)
  1. If you want to store it permanently it can only happen if you generate txt file and then save it
def generateOTP():
    # Declare a string variable
    # which stores all string
    string = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    OTP = ""
    length = len(string)
    for i in range(8):
        OTP  = string[math.floor(random.random() * length)]

    return OTP


otp = generateOTP()

with open('otp.txt', 'w') as O:
    O.write(otp)
  • Related