如果密码违反了这些条件,我将终止程序:
print('Invalid password!')
sys.exit()
我已经被困了几个小时试图添加这些条件......我无法在哪里添加这些条件,无论我在哪里添加它们,我的程序都会终止,即使我输入了有效的密码TT
这是我到目前为止所做的事情(我已经删除了解密部分,以便我可以尝试在之后为自己找出那部分):
# import sys module for termination
import sys
# init
password_out = ''
case_changer = ord('a') - ord('A')
encryption_key = (('a','m'), ('b','h'), ('c','t'), ('d','f'), ('e','g'),
('f','k'), ('g','b'), ('h','p'), ('i','j'), ('j','w'), ('k','e'),('l','r'),
('m','q'), ('n','s'), ('o','l'), ('p','n'), ('q','i'), ('r','u'), ('s','o'),
('t','x'), ('u','z'), ('v','y'), ('w','v'), ('x','d'), ('y','c'), ('z','a'),
('#', '!'), ('@', '('), ('%', ')'), ('0'), ('1'), ('2'), ('3'), ('4'), ('5'),
('6'), ('7'), ('8'), ('9'))
encrypting = True
# get password
password_in = input('Enter password: ')
# perform encryption / decryption
if encrypting:
from_index = 0
to_index = 1
else:
from_index = 1
to_index = 0
case_changer = ord('a') - ord('A')
for ch in password_in:
letter_found = False
for t in encryption_key:
if ('a' <= ch and ch <= 'z') and ch == t[from_index]:
password_out = password_out + t[to_index]
letter_found = True
elif ('A' <= ch and ch <= 'Z') and chr(ord(ch) + 32) == t[from_index]:
password_out = password_out + chr(ord(t[to_index]) - case_changer)
letter_found = True
elif (ch == '#' or ch == '@' or ch == '%') and ch == t[from_index]:
password_out = password_out + t[to_index]
elif (ch >= '0' and ch <= '9') and ch == t[from_index]:
password_out = password_out + ch
# output
if encrypting:
print('Your encrypted password is:', password_out)
else:
print('Your decrypted password is:', password_out)
答案 0 :(得分:2)
不需要正则表达式
import string
import sys
NON_ALPHABETIC_CHARACTERS = {'#', '@', '%'}
DIGITS_CHARACTERS = set(string.digits)
LETTERS_CHARACTERS = set(string.ascii_letters)
def validate_password_1(password,
non_alphabetic_characters=NON_ALPHABETIC_CHARACTERS,
digits_characters=DIGITS_CHARACTERS,
letters_characters=LETTERS_CHARACTERS):
if not any(character in password
for character in non_alphabetic_characters):
err_msg = ('Password should contain at least '
'one non-alphabetic character.')
print(err_msg)
print('Invalid password!')
sys.exit()
if not any(character in password
for character in digits_characters):
err_msg = ('Password should contain at least '
'one digit character.')
print(err_msg)
print('Invalid password!')
sys.exit()
if not any(character in password
for character in letters_characters):
err_msg = ('Password should contain at least '
'one letter character.')
print(err_msg)
print('Invalid password!')
sys.exit()
ALLOWED_CHARACTERS = (NON_ALPHABETIC_CHARACTERS
| DIGITS_CHARACTERS
| LETTERS_CHARACTERS)
def validate_password_2(password,
allowed_characters=ALLOWED_CHARACTERS):
if not all(character in allowed_characters
for character in password):
print('Invalid password!')
sys.exit()
添加了其他消息,以查看给定密码的确切错误
答案 1 :(得分:1)
这肯定不符合您作业的答案,但您可以使用sets
轻松测试这种情况:
import string
alpha = set(string.ascii_lowercase + string.ascii_uppercase)
digits = set(string.digits)
non_alpha = set('#@%')
def is_valid(password):
password_chars = set(password)
# We substract the set of letters (resp. digits, non_alpha)
# from the set of chars used in password
# If any of the letters is used in password, this should be
# smaller than the original set
all_classes_used = all([len(password_chars - char_class) != len(password_chars)
for char_class in [alpha, digits, non_alpha] ])
# We remove all letters, digits and non_alpha from the
# set of chars composing the password, nothing should be left.
all_chars_valid = len(password_chars - alpha - digits - non_alpha) == 0
return all_classes_used and all_chars_valid
for pw in ['a', 'a2', 'a2%', 'a2%!']:
print(pw, is_valid(pw))
# a False
# a2 False
# a2% True
# a2%! False
答案 2 :(得分:0)
检查这两种情况的一种可能方法是执行以下操作:
if password == password.lower() or password == password.upper():
# Reject password here.
我们不打算为你编写其余的加密功能!
答案 3 :(得分:0)
我会使用regexp执行类似的操作:
import re
def test(x):
regexp = r'[@#%]+[0-9]+@*[a-zA-Z]+'
sorted_x = ''.join(sorted(x))
if '@' in sorted_x:
sorted_x = '@%s' % sorted_x
p = re.compile(regexp)
return p.match(sorted_x) is not None
然后该函数给出:
In [34]: test("dfj")
Out[34]: False
In [35]: test("dfj23")
Out[35]: False
In [36]: test("dfj23#")
Out[36]: True
如果您需要一个大写和一个小写字母,可以将正则表达式更改为:
regexp = r'[@#%]+[0-9]+@*[A-Z]+[a-z]+'
排序函数首先放#
和%
,然后是数字,然后是@
,最后是字母(首先是uper case,然后是小写)。因为@
放在中间,所以如果我在字符串中找到一个,我先放一个。
所以最后你想要一个至少包含一个特殊字符的字符串:[@#%]+
,至少一个数字[0-9]+
,可选中间可以有一个@
,最后一封信[a-zA-Z]+
(如果你想要上下[A-Z]+[a-z]+
)。