zoukankan      html  css  js  c++  java
  • 文件、函数、装饰器、迭代器实例

    1. 写函数,计算传入数字参数的和。(动态传参)
    def calc(x, y):
        print(x + y)
    
    
    calc(3, 4)
    View Code

    2.写函数,用户传入修改的文件名,与要修改的内容,执行函数,完成整个文件的批量修改操作

    def calc(filename, content):
        with open(filename, "a", encoding="utf-8") as f:
            f.write(content)
    
    
    calc("test_file", "
    悄悄地,我走了,正如我,悄悄地来")  # test_file:文件名
    View Code

    3.写函数,检查传入的字典的每一个value的长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者。

    def func(x):
        for key in x:
            if len(x[key]) > 2:
                x[key] = x[key][0:2]
            else:
                pass
        print("after change:", x)
    
    
    dic = {"k1": "v1v1", "k2": [11, 22, 33, 44]}
    print("before change:", dic)
    func(dic)
    View Code

    4.写函数,返回一个扑克牌列表,里面有52项,每一项是一个元组例如:[(‘红心’,2),(‘草花’,2), …(‘黑桃A’)]

    a = ["红心", "黑桃", "花草", "方块"]
    b = ["A", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K"]
    
    
    def func(x, y):
        list1 = []  # 新建一个列表
        for i in y:
            for j in x:
                list1.append((j, i))  # 追加
        return list1  # 返回列表
    
    
    res = func(a, b)  # 调用函数
    print(res)
    View Code

    5.写函数,专门计算图形的面积:

    • 其中嵌套函数,计算圆的面积,正方形的面积和长方形的面积
    • 调用函数area(‘圆形’,圆半径) 返回圆的面积
    • 调用函数area(‘正方形’,边长) 返回正方形的面积
    • 调用函数area(‘长方形’,长,宽) 返回长方形的面积
    def area(x, *args):
        def circular():
            s_cir = 3.14 * args[0] ** 2
            return s_cir
    
        def square():
            s_sqa = args[0] ** 2
            return s_sqa
    
        def rectangle():
            s_rec = args[0] * args[1]
            return s_rec
    
        if x == "圆形":
            return circular()
    
        elif x == "正方形":
            return square()
        else:
            return rectangle()
    
    
    res = area("圆形", 3)
    print("圆形", res)
    
    res1 = area("正方形", 3)
    print("正方形", res1)
    
    res2 = area("长方形", 3, 4)
    print("长方形", res2)
    View Code

    6.写函数,传入n个数,返回字典{‘max’:最大值,’min’:最小值}

    dic = {}  # 定义字典
    
    
    def func(*args):
    
        dic["max"] = max(args)  # 最大值
        dic["min"] = min(args)  # 最小值
        return dic
    
    
    res = func(100, 3, 4, 6)
    print(res)
    
    
    输出:
    {'max': 100, 'min': 3}
    View Code

    7.写函数,传入一个参数n,返回n的阶乘

    def func(n):
        if n == 1:
            return 1  # 递归终止条件
        else:
            return n * func(n-1)
    
    
    res = func(7)
    print(res)
    View Code

    8.编写装饰器,为多个函数加上认证的功能(用户的账号密码来源于文件),要求登录成功一次,后续的函数都无需再输入用户名和密码

    用户文件(test_file):
    name   password
    lulu       333
    lanlan    000
    lili         456
    View Code

    代码:

    # 读取文件信息
    user_dic = {}
    with open("test_file", "r", encoding="utf-8") as f:
        for line in f:
            li = line.strip().split()  # 文件内容转为列表
            user_dic[li[0]] = li  # 用户信息存入字典
        print(user_dic)
    status = False  # 用户登录状态
    
    
    # 登录认证
    def login(func):
        def inner():
            global status
            if status is False:  # 用户未登录
                i = 1
                while i <= 3:  # 允许用户登录3次,若3次错误将不能进入该模块
                    username = input("username:")
                    password = input("password:")
                    if username in user_dic and password == user_dic[username][1]:  # 判断用户名与密码是否正确
                        print("welcome")
                        status = True  # 修改用户登录状态
                        break
                    else:
                        print("wrong username or password")
                        i += 1
    
            if status is True:  # 用户已登录
                print("已登录------")
                func()
        return inner
    
    
    @login
    def home():
        print("-----home page------")
    
    
    @login
    def bbs():
        print("-----bbs----")
        print("---畅所欲言---")
    
    
    home()
    bbs()
    View Code

     9.通过生成器写一个日志调用方法, 支持以下功能:

    • 根据指令向屏幕输出日志
    • 根据指令向文件输出日志
    • 根据指令同时向文件&屏幕输出日志

     以上日志格式如下

    2017-10-19 22:07:38 [1] test log db backup 3
    2017-10-19 22:07:40 [2]    user alex login success 
    #注意:其中[1],[2]是指自日志方法第几次调用,每调用一次输出一条日志

     代码:

    import datetime
    count = 0
    
    
    def logger(filename, channel):
        global count
        while True:
            count += 1  # 日志方法第几次调用
            mes = yield  # 接收消息
            info = '%s [%s] %s' % (datetime.datetime.now(), count, mes)
    
            def file():  # 输出到文件
                with open(filename, "a", encoding="utf-8") as f:
                    f.write("
    %s" % info)
    
            def terminal():  # 输出到终端
                print(info)
    
            if channel == "file":  # 输出到文件
                file()
            elif channel == "terminal":  # 输出到终端
                terminal()
            elif channel == "both":  # 都输出
                file()
                terminal()
    
    
    log_obj = logger(filename="test_file", channel='both')
    log_obj.__next__()
    log_obj.send('user alex login success')  # 发送消息
    log_obj.send('test log db backup 3')
    View Code

    10.用map来处理字符串列表name=['alex','wupeiqi','yuanhao','nezha'],把列表中所有人都变成sb,比方alex_sb:

    name = ['alex', 'wupeiqi', 'yuanhao', 'nezha']
    res = list(map(lambda x: x + "_sb", name))
    print(res)
    
    输出:
    ['alex_sb', 'wupeiqi_sb', 'yuanhao_sb', 'nezha_sb']
    View Code

    11.用filter函数处理数字列表num = [1,3,5,6,7,8],将列表中所有的偶数筛选出来:

    num = [1, 3, 5, 6, 7, 8]
    res = list(filter(lambda x: x % 2 == 0, num))
    print(res)
    
    输出:
    [6, 8]
    View Code

    12.如下,每个小字典的name对应股票名字,shares对应多少股,price对应股票的价格

    portfolio = [
        {'name': 'IBM', 'shares': 100, 'price': 91.1},
        {'name': 'AAPL', 'shares': 50, 'price': 543.22},
        {'name': 'FB', 'shares': 200, 'price': 21.09},
        {'name': 'HPQ', 'shares': 35, 'price': 31.75},
        {'name': 'YHOO', 'shares': 45, 'price': 16.35},
        {'name': 'ACME', 'shares': 75, 'price': 115.65}
    ]
    

    (1)计算购买每支股票的总价

    (2)用filter过滤出,单价大于100的股票有哪些

    portfolio = [
        {'name': 'IBM', 'shares': 100, 'price': 91.1},
        {'name': 'AAPL', 'shares': 50, 'price': 543.22},
        {'name': 'FB', 'shares': 200, 'price': 21.09},
        {'name': 'HPQ', 'shares': 35, 'price': 31.75},
        {'name': 'YHOO', 'shares': 45, 'price': 16.35},
        {'name': 'ACME', 'shares': 75, 'price': 115.65}
    ]
    # 计算购买每支股票的总价
    for i in range(len(portfolio)):
        portfolio[i]["total"] = portfolio[i]["shares"] * portfolio[i]["price"]
        print(portfolio[i])
    
    # 单价大于100的股票
    res = list(filter(lambda x: x["price"] > 100, portfolio))
    print("单价大于100的股票:", res)
    
    输出:
    {'name': 'AAPL', 'shares': 50, 'price': 543.22, 'total': 27161.0}
    {'name': 'FB', 'shares': 200, 'price': 21.09, 'total': 4218.0}
    {'name': 'HPQ', 'shares': 35, 'price': 31.75, 'total': 1111.25}
    {'name': 'YHOO', 'shares': 45, 'price': 16.35, 'total': 735.7500000000001}
    {'name': 'ACME', 'shares': 75, 'price': 115.65, 'total': 8673.75}
    单价大于100的股票: [{'name': 'AAPL', 'shares': 50, 'price': 543.22, 'total': 27161.0}, {'name': 'ACME', 'shares': 75, 'price': 115.65, 'total': 8673.75}]
    View Code

    13. 有列表 li = ['alex', 'egon', 'smith', 'pizza', 'alen'], 请将以字母“a”开头的元素的首字母改为大写字母:

    li = ['alex', 'egon', 'smith', 'pizza', 'alen']
    for i in range(len(li)):
        if li[i].startswith("a"):
            li[i] = li[i].capitalize()
    print(li)
    
    输出:
    ['Alex', 'egon', 'smith', 'pizza', 'Alen']
    View Code

    14.有如下程序, 请给出两次调用show_num函数的执行结果,并说明为什么:

    num = 20
    
    
    def show_num(x=num):
        print(x)
    
    
    show_num()
    num = 30
    show_num()
    
    输出:
    20
    20
    View Code

    两次结果都是20,因为语句def show_num(x=num) 中的x=num 相当于设置了一个默认参数,在程序开始执行时就把num=20加载到内存,然后将函数也加载到内存,此时相当于执行了x=20的操作,如果在调用函数时不传值进来,这个x将默认就是20,可以再看如下程序:

    num = 20
    
    
    def show_num(x=num):
        print(x)
    
    
    show_num()
    num = 30
    show_num(num)  # 函数调用时传了参数
    
    输出:
    20
    30
    View Code

    15.有列表 li = ['alex', 'egon', 'smith', 'pizza', 'alen'], 请以列表中每个元素的第二个字母倒序排序

    li = ['alex', 'egon', 'smith', 'pizza', 'alen']
    li1 = sorted(li, key=lambda x: x[1], reverse=True)
    print(li1)
    View Code

    16.有名为test_file的文件,其内容如下,请删除第三行;

    昔人已乘黄鹤去,此地空余黄鹤楼。
    黄鹤一去不复返,白云千载空悠悠。
    晴川历历汉阳树,芳草萋萋鹦鹉洲。
    日暮乡关何处是?烟波江上使人愁。
    li = []
    with open("test_file", "r", encoding="utf-8") as f:  # 读文件
        for line in f:
            li.append(line.strip())
    del li[2]  # 删除第三行
    with open("test_file", "w", encoding="utf-8") as f:  # 写文件
        for i in li:
            f.write("%s
    " % i)
    View Code

    17.有名为test_file的文件,其内容格式如下,写一个程序,删除id为100003的行

    pizza,100001
    alex, 100002
    egon, 100003
    

    代码:

    with open("test_file", "r+", encoding="utf-8") as f:  # 打开文件
        li = []
        for line in f:
            li.append(line.strip().split(","))  # 追加
        print(li)
        f.seek(0)  # 回到文件头
        f.truncate()  # 清空文件
        for i in range(len(li)):
            if li[i][1].strip() == "100003":
                del li[i]  # 删除
            else:
                f.write("%s
    " % ",".join(li[i]))  # 重新写入
    View Code

    18.有名为test_file的文件,其内容格式如下,写一个程序,判断该文件中是否存在"alex", 如果没有,则将字符串"alex"添加到该文件末尾,否则提示用户该用户已存在;

    pizza
    egon
    

     代码:

    with open("test_file", "r+", encoding="utf-8") as f:
        for line in f:
            if "alex" in line:
                print("existed")
                break
        else:
            f.write("
    alex")
    View Code

    19.接17题,写一个程序,将id为100002的用户名修改为alex li
    代码:

    with open("test_file", "r+", encoding="utf-8") as f:
        li = []
        for line in f:
            li.append(line.strip().split(","))  # 追加
        print(li)
        f.seek(0)  # 回到文件头
        f.truncate()  # 清空文件
        for i in range(len(li)):
            if li[i][1].strip() == "100002":
                li[i][0] = "alex li"  # 修改
            f.write("%s
    " % ",".join(li[i]))  # 重新写入
    View Code

    20.写一个计算每个程序执行时间的装饰器

    import time
    
    
    def run(func):
        def inner():
            start = time.time()  # 记录开始运行时间
            func()
            stop = time.time()  # 结束运行时间
            print("the program run time:{} S".format(stop - start))
        return inner
    
    
    @run
    def test():
        time.sleep(1)  # 睡1秒
        print("in the test")
    
    
    test()
    
    输出:
    in the test
    the program run time:1.0000572204589844 S
    View Code

    21.写一个摇骰子游戏,要求用户压大小,赔率一赔一。要求:三个骰子,摇大小,每次打印摇骰子数。

    import random
    
    
    # 摇骰子
    def roller():
        li = random.sample(range(1, 7), 3)  # 生成三个随机数
        print("骰子点数:", li)
        return sum(li)
    
    
    # 定义大小
    def roll_result(total1):
        small = 3 <= total1 <= 9
        big = 9 < total1 <= 18
        if small:
            return ""
        elif big:
            return ""
    
    
    # 开始游戏
    money = 1000
    item = ["", ""]
    while money > 0:
        money_bet = input("押注金额(输入整数):")
        choice = input("押大小(大/小)、退出请输入'q':")
        if choice in item:
            total = roller()  # 获取骰子点数
            res = roll_result(total)   # 判断是大还是小
            if res == choice:
                money = money + int(money_bet)
                print("恭喜你押对了,获得奖励{}元,你的余额为{}元".format(money_bet, money))
            else:
                money = money - int(money_bet)
                print("你押错了,输掉{}元,余额{}元".format(money_bet, money))
        elif choice == "q":
            break
        else:
            print("输入错误")
    else:
        print("你的钱输光了,game over")
    View Code


    22.写函数,检查用户传入的对象(字符串、列表、元组)的每一个元素是否含有空内容。

    def file_k(file):
        n = 0
        for i in file:
            if i == " ":
                n += 1
        print("有%s个空" % n)
    
    
    file_k([1, " ", 2, 3, " "])
    file_k((1, " ", 2, 3))
    file_k("hello world")
    
    输出:
    有2个空
    有1个空
    有2个空
    View Code

     

     

     

     

     

  • 相关阅读:
    解决Tomcat请求中文乱码的问题
    Bootstrap 兼容 IE
    Spring+MyBatis多数据源配置实现
    年度总结与计划
    AJAX 提交表单以及文件上传
    移动端Web开发调试工具:Chrome DevTools
    SpringMVC 之 @ResponseBody 和 @RequestBody
    goland快键键防忘
    MySQL日志文件影响数据库的各种类型活动
    何为云计算
  • 原文地址:https://www.cnblogs.com/yanlin-10/p/9018894.html
Copyright © 2011-2022 走看看