我正在尝试学习如何使用while循环来记录每次不正确的尝试,包括时间和日期以及原因。输出还应该显示文件的错误/弱尝试。
MIN_PASSWORD_LENGTH = 6
MAX_PASSWORD_LENGTH = 14
PASSWORD_LOG_FILE = "password_log_your_name.txt"
password = input("Enter your password: ")
password_length = len(password)
while password_length >= MIN_PASSWORD_LENGTH and password_length <= MAX_PASSWORD_LENGTH:
PASSWORD_LOG_FILE = open("password_log_your_name.txt", "a")
PASSWORD_LOG_FILE.write(password)
PASSWORD_LOG_FILE.write("\n")
import datetime
my_date = datetime.datetime.today()
print(str(datetime.datetime.today()))
todays_date = my_date.strftime('%A %B %d, %Y')
print(f"Date: {todays_date:s}\n")
PASSWORD_LOG_FILE.close()
if password.isalpha():
print("Your password is weak! It only contains letters.")
elif password.isnumeric():
print("Your password is weak! It only contains numbers.")
else:
print("Your password is strong! It contains letters and numbers.")
break
我不确定该如何进行?我知道我需要使用datetime记录他们尝试尝试的时间,并使用.write写入文件。无论如何,我都认为,但是我对如何将所有内容组合在一起感到困惑。
答案 0 :(得分:1)
考虑到您需要存储所有尝试,无论是week
还是strong
,我建议使用while True
并按以下顺序进行:
import datetime
ps_file = "list.txt"
while True:
p_inp = input("Enter your password OR Press N to quit")
if p_inp.lower() == 'n':
exit()
with open(ps_file, 'a+') as fileObj:
if p_inp.isalpha():
print("Your password is weak! It only contains letters.")
p_strength = "weak"
elif p_inp.isnumeric():
print("Your password is weak! It only contains numbers.")
p_strength = "weak"
else:
print("Your password is strong! It contains letters and numbers.")
p_strength = "strong"
fileObj.write("Date: {}".format(datetime.datetime.today()) + "\n")
fileObj.write("Password: {}".format(p_inp + "\n"))
fileObj.write("Password strength: {}".format(p_strength) + "\n")
输出(list.txt):
Date: 2019-03-27 11:41:10.107696
Password: weekattempt
Password strength: weak
Date: 2019-03-27 11:41:14.388402
Password: strongattempt1
Password strength: strong
Date: 2019-03-27 11:41:26.812254
Password: strongatempt2
Password strength: strong
编辑:
如果您使用单独的函数进行密码验证和文件写入,那就更好了:
import datetime
ps_week = "WEEK"
ps_strong = "STRONG"
ps_week_a = "Your password is weak! It only contains letters."
ps_week_b = "Your password is weak! It only contains number."
ps_strong_a = "Your password is strong! It contains letters and numbers."
def writeTofile(fileObj, p_strength, p_text):
fileObj.write("Date: {}".format(datetime.datetime.today()) + "\n")
fileObj.write("Password: {}".format(p_inp + "\n"))
fileObj.write("Password strength: {}".format(p_strength) + "\n")
fileObj.write("Prompt: {}".format(p_text) + "\n")
def p_validate(p_inp, fileObj):
if p_inp.isalpha():
writeTofile(fileObj, ps_week, ps_week_a)
elif p_inp.isnumeric():
writeTofile(fileObj, ps_week, ps_week_b)
else:
writeTofile(fileObj, ps_strong, ps_strong_a)
while True:
p_inp = input("Enter your password OR Press N to quit")
if p_inp.lower() == 'n':
exit()
with open('list.txt', 'a+') as fileObj:
p_validate(p_inp, fileObj)
输出:
Date: 2019-03-27 11:51:36.512665
Password: weakweak
Password strength: WEAK
Prompt: Your password is weak! It only contains letters.
Date: 2019-03-27 11:51:55.704586
Password: strongatttempt1
Password strength: STRONG
Prompt: Your password is strong! It contains letters and numbers.
Date: 2019-03-27 11:52:50.609155
Password: strongattempt2
Password strength: STRONG
Prompt: Your password is strong! It contains letters and numbers.
答案 1 :(得分:0)
不确定您为什么计划以这种方式做任何事情。但是根据您的问题,这应该是顺序。您可能希望使用使用with open('filename','a') as PASSWORD_LOG_FILE:
的上下文处理来进行文件处理。这只是为了演示您要执行的操作的序列。试图使用大多数代码。
import datetime
MIN_PASSWORD_LENGTH = 6
MAX_PASSWORD_LENGTH = 14
PASSWORD_LOG_FILE = "password_log_your_name.txt"
password = input("Enter your password: ")
password_length = len(password)
if password_length >= MIN_PASSWORD_LENGTH and password_length <= MAX_PASSWORD_LENGTH:
if password.isalpha():
print("Your password is weak! It only contains letters.")
elif password.isnumeric():
print("Your password is weak! It only contains numbers.")
else:
print("Your password is strong! It contains letters and numbers.")
PASSWORD_LOG_FILE = open("password_log_your_name.txt", "a")
PASSWORD_LOG_FILE.write(password)
PASSWORD_LOG_FILE.write("\n")
my_date = datetime.datetime.today()
print(str(datetime.datetime.today()))
todays_date = my_date.strftime('%A %B %d, %Y')
PASSWORD_LOG_FILE.write(todays_date)
PASSWORD_LOG_FILE.write("\n")
print(f"Date: {todays_date:s}\n")
PASSWORD_LOG_FILE.close()