第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)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。