4 回答

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!')

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!')

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!')

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())。
添加回答
舉報