我需要制作一个密码重置程序,让用户输入两次新密码,以便计算机知道用户在输入新密码时没有犯任何错误。它也应该只接受密码,如果它是八个字符或更少,并包括小写和大写字母。 这是我到目前为止所做的:
import os
import time
def main():
while True:
PassWord = input ("Enter Password: ")
for c in s:
if c.islower():
print c
Password == :
time.sleep(1)
print ("Login successful!")
logged()
else:
print ("Please try again")
def logged():
time.sleep(1)
print ("Welcome to ----")
main()
答案 0 :(得分:0)
您的程序中存在许多语法错误。值得重新考虑核心逻辑。
import os
import time
import getpass
def logged():
time.sleep(1)
print ("Welcome to ----")
def checklower(pw):
for c in pw:
if c.islower():
return True
def checkupper(pw):
for c in pw:
if c.isupper():
return True
def main():
while True: # having this loop because you have it too
password_1 = getpass.getpass()
print "One more time"
password_2 = getpass.getpass()
if (password_1 != password_2) or not (checkupper(password_1)) or not (checklower(password_2)) or not (len(password_1) >= 8):
print "Please try again"
continue
else:
logged()
break
main()
这是您需要的超级快速和肮脏的实现
答案 1 :(得分:0)
我不确定哪个身份验证系统允许八个或更少的字符,但当然,我们可以让它工作。
在处理密码时,您不应该使用普通input
。在这种情况下,任何看着你肩膀的人都可以轻松获得你的密码。您应该使用getpass
代替,这是标准库的一部分。函数将检查密码是否满足条件并返回布尔值。这不是最佳实现,但它适用于此问题。希望这会有所帮助。
from getpass import getpass
def check_password(password):
if len(password) > 9:
return False
for character in password:
if character.isupper():
return True
def main():
while True:
password = getpass()
if password == getpass():
if check_password(password):
print('Successfully logged in.')
break
else:
print('Try again!')
if __name__ == '__main__':
main()
答案 2 :(得分:0)
import time
import re
password=''
password2=''
def validate_password(password):
if len(password) <8:
print 'returning 1'
return 1
if not re.search('[0-9]',password):
return 2
if not re.search('[a-z]',password):
return 3
if not re.search('[A-Z]',password):
return 4
return 0
def check_password(password,password2):
if password==password2:
return True
else:
return False
def get_password():
password=raw_input('Enter password:')
flag=validate_password(password)
if flag==0:
password2=raw_input('Re-Enter Password:')
flag2=check_password(password,password2)
if flag2==True:
return True
else:
print 'Passwords do not match!!'
return False
elif flag==1:
print 'Password must be of 8 characters'
return False
elif flag==2:
print 'Password must contain a number'
return False
elif flag==3:
print 'Password must include a small letter'
return False
elif flag==4:
print 'Password must also include a small letter'
return False
def logged():
print 'Logging in...'
time.sleep(5)
print 'Successfully logged in'
def main():
flag=get_password()
if flag==True:
logged()
else:
while True:
## print ''
get_password()
main()