![](/img/trans.png)
[英]How can I pass a variable from function to function in Python (Kivy)
[英]How can I pass a variable to a function without continuing with the original function it was called from in Python?
當談到 Python 時,我不是專家,我正在嘗試將幾個功能放在一起來執行以下操作:
但是,即使我提供的新密碼太短或太長,它也會跳回原來的 function 並執行代碼的 rest。 我知道代碼尚未完成,但我知道我沒有正確執行。 請問有什么建議嗎? 此外,當涉及到提供密碼/用戶名的最終失敗嘗試並且我希望應用程序說“嘗試次數過多”時,最好放在哪里?
代碼的 rest 在理想條件下幾乎可以正常工作(散列、OTP、SQL,...)
到目前為止,這是我的代碼:
import sqlite3
import hashlib
import pyotp
import sys
from password_validator import PasswordValidator
connection = sqlite3.connect("localDB.db")
cursor = connection.cursor()
cursor.execute("DROP TABLE passwords")
cursor.execute("CREATE TABLE passwords (name, password)")
cursor.execute("INSERT INTO passwords VALUES ('Tomas','password123')")
def OTP():
totp = pyotp.TOTP('base32secret3232')
print("This is your one-time passcode: ")
print(totp.now())
userOTP = input("Please provided your passcode: ")
otpass = totp.verify(userOTP)
if otpass == True:
print("You are now logged in")
sys.exit()
else:
print("Access Forbidden")
sys.exit()
def correctUsername(username):
print("Account found, please provide your password: ")
retry = 0
while retry<3:
userPassword = input()
cursor.execute('SELECT password FROM passwords WHERE name=?', [username])
match = cursor.fetchone()
dbPassword = match[0]
# print(dbPassword)
hashedCorrectPassword = hashlib.sha256(str(dbPassword).encode('utf-8')).hexdigest()
hashedInputPassword = hashlib.sha256(str(userPassword).encode('utf-8')).hexdigest()
#print(hashedCorrectPassword)
#print(hashedInputPassword)
try:
if hashedInputPassword == hashedCorrectPassword:
print("That's correct, welcome.")
OTP()
else:
print("That's incorrect, please try again.")
retry += 1
continue
except:
break
print("Maximum tries exceeded")
def passwordValidator(registrationPassword):
schema = PasswordValidator()
schema.min(8)
schema.max(15)
schema.has().uppercase()
schema.has().lowercase()
validatePassword = schema.validate(registrationPassword)
if validatePassword == True:
print("Good password")
else:
print("Try again - not strong enough")
def userRegistration():
compliance = bool
print("Register here")
registrationName = input("What is your name?")
print("Your name is ", registrationName)
registrationPassword = input("What is your password?")
passwordValidator(registrationPassword)
cursor.execute("INSERT INTO passwords VALUES (?,?)",(registrationName, registrationPassword))
print("User successfully registered, please login with your new details: ")
passwordDatabase()
def passwordDatabase():
print("Please enter your username: ")
retry = 0
while retry<3:
try:
username = input()
if username == "YES":
userRegistration()
else:
cursor.execute('SELECT name FROM passwords WHERE name=?', [username])
match = cursor.fetchone()
if match is None:
print("No account found, please try again, or type YES for registration")
retry += 1
continue
else:
correctUsername(username)
break
except:
break
print("Maximum tries exceeded.")
一種選擇是讓passwordValidator
既要求又返回密碼。 不過,這里的關鍵步驟是將這個詢問過程放在一個循環中。 像這樣的東西
def passwordValidator():
while True:
valid_password = True
password = input("What is your password?")
#
#
# validation steps here, if it is invalid, set valid_password to False
if valid_password:
return password
如果輸入了無效密碼, while
循環將繼續,並再次提示用戶輸入密碼。
然后你可以做
def userRegistration():
compliance = bool
print("Register here")
registrationName = input("What is your name?")
print("Your name is ", registrationName)
registrationPassword = passwordValidator(registrationPassword)
cursor.execute("INSERT INTO passwords VALUES (?,?)",(registrationName, registrationPassword))
print("User successfully registered, please login with your new details: ")
passwordDatabase()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.