PYTHON_AI_LESSON05

 1.斐波那契数列

def fibonacci(n):
    """
    计算斐波那契数列中的第n个数字。

    参数:
    n -- 非负整数,表示斐波那契数列的位置

    返回:
    int -- 斐波那契数列中位置为n的数字
    """
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        a, b = 0, 1
        m=0
        for _ in range(2, n + 1):
            a, b = b, a + b
        return b

print(fibonacci(3))

 

 

 2.密码校验

import re


def has_number(pwd):
    return bool(re.search(r'[0-9]', pwd))


def has_upper(pwd):
    return bool(re.search(r'[A-Z]', pwd))


def has_lower(pwd):
    return bool(re.search(r'[a-z]', pwd))


def has_special(pwd):
    return bool(re.search(r'[^A-Za-z0-9]', pwd))


def passwd_check(pwd):
    """密码校验函数"""
    if len(pwd) < 8:
        return "弱密码"

    has_num = has_number(pwd)
    has_upp = has_upper(pwd)
    has_low = has_lower(pwd)
    has_spe = has_special(pwd)

    # 弱密码:都是数字,或者都是大写字母/小写字母组成的密码
    if (has_num and not (has_upp or has_low or has_spe)) or \
            (has_upp and not (has_num or has_low or has_spe)) or \
            (has_low and not (has_num or has_upp or has_spe)):
        return "弱密码"

    # 中等密码:[数字、大写字母] 或者 [数字、小写字母] 或者 [大写字母、小写字母] 组成的密码
    if (has_num and has_upp and not (has_low or has_spe)) or \
            (has_num and has_low and not (has_upp or has_spe)) or \
            (has_upp and has_low and not has_spe):
        return "中密码"

    # 强密码:[数字、大写字母] 或者 [数字、小写字母] 或者 [大写字母、小写字母] 并结合特殊符号组成的密码
    if has_num and has_upp and has_low and has_spe:
        return "强密码"

    # 其他情况默认为中等密码
    return "中密码"


try:
    password = input("请输入密码:")
    result = passwd_check(password)
    print(result)
except Exception as e:
    print(f"发生错误:{e}")

3.加密解密

valve = str(input("请输入加密明文:"))

def encrypt(valve,n=4):
    ep = ""
    for i in str(valve):

        # 转换为ascii码
        ac = ord(i)
        # 错位
        ac = ac + n
        # 转换为字符
        _ac = chr(ac)
        # 拼接字符
        ep += _ac
    return ep
def decrypt(password, n=4):
    """解密函数"""
    pwd = ""
    for i in str(password):
        ac = ord(i)
        ac -= n
        _ac = chr(ac)
        pwd += _ac
    return pwd

pd = input("请输入明文数据:")
print("加密后的数据:", encrypt(pd, 5))

pd2 = input("请输入密文数据:")
print("解密后的数据:", decrypt(pd2, 5))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值