第07章課后項目

7.18.1


import re

def testPassword(password):

    password = str(password)

    upperRegex = re.compile(r'[A-Z]')

    lowerRegex = re.compile(r'[a-z]')

    numRegex  = re.compile(r'[0-9]')

    if len(password) < 8:

        print('The password must be more than 8 digits.')

        return

    elif not upperRegex.search(password):

        print('The password must contain uppercase characters.')

        return

    elif not lowerRegex.search(password):

        print('The password must contain lowercase characters.')

        return

    elif not numRegex.search(password):

        print('The password must contain digits characters.')

        return

    print('The password is strong enough!')

    return

while True:

    password = input('Please input your password:\n')

    testPassword(password)

7.18.2


import re

def reStrip(string, words):

    spaceRegex = re.compile(r'^(\s*)(.*[^\s])(\s*)$')

    wordRegex  = re.compile(r'^([%s]*)(.*[^%s])([%s]*)$'

                            %(words, words, words))

    mo = spaceRegex.search(string)

    if (mo.group(1) or mo.group(3)):

        print('''The string has white space, so the result is: \n'''

              + mo.group(2))

        return

    else:

        mo = wordRegex.search(string)

        print('''The string has no white space, so the result is: \n'''

              + mo.group(2))

        return

while True:

    string = input('Please input the string:\n')

    words  = input('Please input the unwanted words:\n')

    reStrip(string, words)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容