1.项目描述
2.判断字符串:含有数字,字母等
3.break与continue区别
for i in range(100):
if i%2==0:
break
print(i)
for i in range(100):
if i%2==0:
continue
print(i)
4.文件操作
w 每次都重写一遍
a 保存上一次的信息
"""
auther:
version:v6.0 python3.0
date:2019.4.6
fuction:Determine password strength
Use:1.break 2.Limit the number of passwords set
3.Save the password and the corresponding strength to the file
"""
def check_number_exist(password_srt):
has_number = False
for c in password_srt:
if c.isnumeric():
has_number = True
break
return has_number
def check_letter_exist(password_srt):
has_letter = False
for c in password_srt:
if c.isalpha():
has_letter = True
break
return has_letter
def main():
try_time = 5
while try_time > 0:
password = input('Please enter a password:')
strengtg_level = 0
if len(password) >= 8:
strengtg_level += 1
else:
print ('Password length is at least 8 digits!')
if check_number_exist(password):
strengtg_level += 1
else:
print ('Password must contain numbers!')
if check_letter_exist(password):
strengtg_level += 1
else:
print ('Password must contain letter!')
s = 0
if strengtg_level == 1:
s = 'Weak password'
elif strengtg_level == 2:
s= 'Middle password'
elif strengtg_level == 3:
s = 'Strong password'
f = open('password_3.0.txt','a')
f.write('password:{},strength:{}\n'.format(password,s))
f.close()
if strengtg_level == 3:
print ('Congratulations, the password strength is qualified.')
# f = open('password_3.0.txt','a')
# f.write(password)
# f.close
break
else:
print ('Unsatisfactory password strength.')
try_time -= 1
print()
if try_time <= 0:
print('Too many attempts to set password failed.')
if __name__=='__main__':
main()
5.面向对象编程
"""
auther:
version:v6.0 python3.0
date:2019.4.12
fuction:Determine password strength
New additions:
1.break
2.Limit the number of passwords set
3.Save the password and the corresponding strength to the file
4.Read the password saved in the file
5.Define a password class
6.Define a file_tool
"""
class PasswordTool:
def __init__(self,password):
#Class attribute
self.passwprd = password
self.strength_level = 0
def process_password(self):
if len(self.passwprd) >= 8:
self.strength_level += 1
else:
print ('Password length is at least 8 digits!')
if self.check_number_exist():
self.strength_level += 1
else:
print ('Password must contain numbers!')
if self.check_alpha_exist():
self.strength_level += 1
else:
print ('Password must contain letter!')
#Class method
def check_number_exist(self):
has_number = False
for c in self.passwprd:
if c.isnumeric():
has_number = True
break
return has_number
def check_alpha_exist(self):
has_letter = False
for c in self.passwprd:
if c.isalpha():
has_letter = True
break
return has_letter
class FileTool:
def __init__(self,filepath):
self.filepath = filepath
def write_to_file(self,line):
f = open(self.filepath,'a')
f.write(line)
f.close()
def read_from_file(self):
f = open(self.filepath,'r')
lines = f.readlines()
f.close()
return lines
def main():
try_time = 5
filepath = 'password_6.0.txt'
file_tool = FileTool(filepath)
while try_time > 0:
password = input('Please enter a password:')
#Instantiated object
password_tool = PasswordTool(password)
password_tool.process_password()
line = 'password:{},strength:{}\n'.format(password, password_tool.strength_level)
file_tool.write_to_file(line)
if password_tool.strength_level == 3:
print ('Congratulations, the password strength is qualified.')
# f = open('password_3.0.txt','a')
# f.write(password)
# f.close
break
else:
print ('Unsatisfactory password strength.')
try_time -= 1
print()
if try_time <= 0:
print('Too many attempts to set password failed.')
lines = file_tool.read_from_file()
print(lines)
if __name__=='__main__':
main()