定义函数的作业

第一题

题目描述

斐波那契数列是一种特殊的数列,很多生活和企业场景中都能见到满足该数列的数据排列方式;现在要求你封装一个用于获取斐波那契数列数据的功能函数,用于底层数据支持;

输入描述

输入一个整数n

输出描述

输出位置为n的斐波那契数列对应的数据

示例

输入:1

输出:1


输入:3

输出:2


输入:8

输出:21

def fibonacci_loop(n):
    if n <= 0:
        raise ValueError("输入值需要大于0")
    a, b = 0, 1
    for _ in range(2, n + 1):
        a, b = b, a + b
    return b
#测试函数
date = input("请输入数据:")
print(fibonacci_loop())

第二题

题目描述

按照用户输入密码的字符,判断用户的密码强度,基本要求如下

  • 弱:都是数字,或者都是大写字母/小写字母组成的密码
  • 中等:[数字、大写字母] 或者 [数字、小写字母] 或者 [大写字母、小写字母] 或者 [大小写字母、数字]组成的密码
  • 强:[数字、大写字母] 或者 [数字、小写字母] 或者 [大写字母、小写字母]并结合特殊符号组成的密码

输入描述

用户输入一个字符串密码

输出描述

输出密码强度等级

示例

输入:abcdef

输出:弱密码


输入:abc123

输出:中等强度


输入:Abc123%

输出:强密码

def check_password_strength(password):
    has_digit = any(char.isdigit() for char in password)
    has_upper = any(char.isupper() for char in password)
    has_lower = any(char.islower() for char in password)
    has_special = any(char in "!@#$%^&*()-_=+[]{}|;:',.<>?/~`" for char in password)

    if not (has_digit or has_upper or has_lower or has_special):
        return "无效"

    if (has_digit and (has_upper or has_lower)) or (has_upper and has_lower):
        if has_special:
            return "强密码"
        else:
            return "中等密码"
    elif has_digit or has_upper or has_lower:
        return "弱密码"
    else:
        return "无效"


# 测试函数
passwords = input("请输入您的密码:")
print(check_password_strength(passwords))

第三题

题目描述

编写一个函数,实现密码的错位加密

编写一个函数,实现密码的错位解密

输入描述

输入一个字符串明文密码;

输入一个字符串密文数据;

输出描述

打印展示加密后的字符串密文

打印展示解密后的明文字符串

示例

输入:

abc # 明文

def # 密文

输出:

cde # 错位密文(具体错位可以自定义)

cde # 解密明文(具体错位可以自定义)

def encrypt(password,n=4) :
    """加密的函数"""
    ep = ""
    for i in str(password) :
        #转换为ascii码
        ac = ord(i)
        if ac > 126 :
            ac = ac % 126 +32
        if ac < 32 :
            ac = ac +32
        #错位
        ac += n
        #将ascii码转为字符串
        _ac = chr(ac)
        #把加密后的密码拼接到ep中
        ep += _ac
    return ep


def decrypt(password,n=4) :
    """解密的函数"""
    dp = ""
    for i in str(password) :
        #转化为ascii码
        ac = ord(i)
        if ac > 126 :
            ac = ac % 126 +32
        if ac < 32 :
            ac = ac +32
        #错位解密
        ac -= n
        #把ascii码转换为字符串
        _ac = chr(ac)
        #拼接
        dp += _ac
    return dp

pd1 = str(input("请输入要加密的密码:"))
print("加密后的密码为:",encrypt(pd1))
pd2 = str(input("请输入要解密的密码:"))
print("解密后的密码为:",decrypt(pd2))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值