zoukankan      html  css  js  c++  java
  • [ python ] 练习作业

    1、写函数,检查获取传入列表或元组对象的所有奇数位索引对应的元素,并将其作为新列表返回给调用者。

    lic = [0, 1, 2, 3, 4, 5]
    def func(l):
        return l[1::2]
    print(func(lic))

    2、写函数,判断用户传入的对象(字符串、列表、元组)长度是否大于5。

    def func(s):
        if len(s) > 5:
            print('%s > 5' % s)
        elif len(s) <= 5:
            print('%s <= 5' % s)

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

    def func(n):
        return n[:2]

    4、写函数,计算传入字符串中【数字】、【字母】、【空格] 以及 【其他】的个数,并返回结果。

    context = input('>>>')
    
    def func(arg):
        dic = {'数字':0, '字母':0, '空格':0, '其他':0}
        for i in arg:
            if i.isdigit():
                dic['数字'] += 1
            elif i.isalpha():
                dic['字母'] += 1
            elif i.isspace():
                dic['空格'] += 1
            else:
                dic['其他'] += 1
        return dic
    
    print(func(context))

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

    l = ['a', ' b', 'c ', 'hel   lo', 1, 2, 3]
    def func(arg):
        for i in arg:
            i = str(i)
            if ' ' in i:
                print('%s 内有空格' % i)
    
            else:
                print(i)
    func(l)

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

    dic = {1: 123, 'a': 'hello', 'b':['world', 'nice', 'bigbang']}
    
    def func(dic):
        for k, v in dic.items():
            if not isinstance(v, (int, float, bool)):
                dic[k] = v[:2]
        return dic
    
    print(func(dic))

    7、写函数,接收两个数字参数,返回比较大的那个数字。

    print(max(1, 10))

    8、写函数,用户传入修改的文件名,与要修改的内容,执行函数,完成整个文件的批量修改操作(进阶)。

    import os
    
    file_name = input('文件名:')
    be_modify = input('要修改的内容:')
    af_modify = input('要替换的内容:')
    
    def func(file, be_f, af_f):
        with open(file, 'r', encoding='utf-8') as read_f, open(file+'_new', 'w', encoding='utf-8') as write_f:
            for line in read_f:
                if be_f in line:
                    new_line = line.replace(be_f, af_f)
                    write_f.write(new_line)
                else:
                    write_f.write(line)
        os.remove(file_name)
        os.rename(file_name + '_new', file_name)
    
    
    func(file_name, be_modify, af_modify)

    9、写一个函数完成三次登陆功能,再写一个函数完成注册功能

    def regist():
        while True:
            user = input('user:').strip()
            if not user: continue
            else:
                break
        pwd = input('pwd:').strip()
        dic = ('注册账号:{}, 密码:{}'.format(user, pwd))
        return dic
    
    # print(regist())
    
    
    def login():
        count = 1
        while count < 4:
            username = input('username:')
            password = input('password:')
            if username == 'hkey' and password == '123':
                print('登录成功.')
                return 
            else:
                print('登录失败.')
            count += 1
    
    login()
  • 相关阅读:
    bootstrap精简教程
    mvc中EditorFor TextBoxFor什么区别
    jQueryEasyUI DateBox的基本使用
    visual studio 2012如何彻底删除TFS上的团队项目
    清除TFS版本控制信息
    在Vs2012 中使用SQL Server 2012 Express LocalDB打开Sqlserver2012数据库
    display & visibility区别
    SQL localdb 连接字符串
    cpio备份命令
    tar备份工具
  • 原文地址:https://www.cnblogs.com/hukey/p/9821812.html
Copyright © 2011-2022 走看看