![](/img/trans.png)
[英]How to write a python function to check if a user input for a password is not strong?
[英]python check user input for email password loop
我想讓我的腳本在繼續其余腳本之前先驗證電子郵件密碼是否正確。 我敢肯定這只是一個簡單的循環,但我想不出一種簡單的方法。 現在我的腳本是:
import arcpy, os, sys, time, subprocess, collections, datetime, smtplib
from datetime import datetime
print("This script will check your email password to verify it is correct.")
usr = "your_email@gmail.com"
print("Please enter the password for " + usr + ":")
psw = raw_input()
def passwordcheck(usr,psw):
server=smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(usr,psw)
server.quit()
print("Thank you. Password was correct.")
try:
passwordcheck(usr,psw)
except:
print("Password was incorrect.")
我想要一個簡單的循環,讓用戶3次嘗試輸入正確的密碼,然后如果未輸入正確的密碼,則終止該腳本。
這將更有意義。
def passwordcheck(usr,psw):
server=smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
try:
server.login(usr,psw)
ret = True
except:
ret = False
server.quit()
return ret
for i in range(3):
psw = raw_input("Please enter the password for " + usr + ": ")
if passwordcheck(usr,psw) is False:
print("Password was incorrect.")
else:
print("Thank you. Password was correct.")
break
因為在接近錯誤源的位置處處理錯誤並采取相應的措施應被視為最佳實踐。 同樣,考慮到在函數調用之外您保留了Password was incorrect
,您可能還應該將正輸出也保持關閉(始終將相似的輸出保持彼此靠近)。
因此,請處理passwordcheck
的錯誤並處理該函數的返回碼。
對於SO來說,這可能不是一個典型的問題,因為您的初始代碼沒有錯,因為更多的代碼回顧使您想到了它。
這樣的事情應該做到這一點:
def passwordcheck(usr)
print("This script will check your email password to verify it is correct.")
usr = "your_email@gmail.com"
for i in range(3):
print("Please enter the password for " + usr + ":")
psw = raw_input()
try:
server=smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(usr,psw)
server.quit()
print "Thank you. Password was correct."
break
except:
if i < 3:
print "Password was incorrect. Try again:"
continue
print "Password was incorrect. 3 times"
break
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.