我有一個功能,每次運行程式時都會生成一個新密碼。我想知道是否可以將生成的密碼存盤在變數或函式中。我怎樣才能做到這一點?代碼如下:
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())
我不知道如何解決這個問題。任何幫助都會很棒!
謝謝
uj5u.com熱心網友回復:
- 兄弟,它已經在你里面了 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)
- 如果要永久存盤它,只有在生成 txt 檔案然后保存時才會發生
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/521507.html
上一篇:使用包含兩個串列的資料框使用matplotlib創建繪圖
下一篇:選擇元組給定區間的兩個元素
