4 回答
![?](http://img1.sycdn.imooc.com/53339fdf00019de902200220-100-100.jpg)
TA贡献1853条经验 获得超9个赞
最直观的方法就是检查每个字符。
if not all(c.isalnum() or c in '_!' for c in password): print('Your password must not include any special characters or symbols!')
![?](http://img1.sycdn.imooc.com/533e4c9c0001975102200220-100-100.jpg)
TA贡献1826条经验 获得超6个赞
这是一种方法。!将和替换_为空字符串,然后用 进行检查isalnum()。
password = input('Enter a password: ')
pwd = password.replace('_', '').replace('!', '')
if pwd.isalnum() and ('_' in password or '!' in password):
pass
else:
print('Your password must not include any special characters or symbols!')
![?](http://img1.sycdn.imooc.com/5458453d0001cd0102200220-100-100.jpg)
TA贡献1772条经验 获得超5个赞
检查它的另一种方法是使用正则表达式
import re
x = input('Enter a password: ')
t = re.fullmatch('[A-Za-z0-9_!]+', x)
if not t:
print('Your password must not include any special characters or symbols!')
![?](http://img1.sycdn.imooc.com/5458692c00014e9b02200220-100-100.jpg)
TA贡献1794条经验 获得超8个赞
def is_pass_ok(password):
if password.replace('_', '').replace('!','').isalnum():
return True
return False
password = input('Enter a password: ')
if not is_pass_ok(password):
print('Your password must not include any special characters or symbols!')
通过删除所有允许的特殊字符,即_和!:
password.replace('_', '').replace('!','')
它仅检查字母数字字符 ( .isalnum())。
添加回答
举报