day9函数作业详解

1.day9题目

1,整理函数相关知识点,写博客。

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

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

4,写函数,检查传入列表的长度,如果大于2,将列表的前两项内容返回给调用者。

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

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

7,写函数,检查传入字典的每一个value的长度,如果大于2,那么仅保留value前两个长度的内容,并将新内容返回给调用者。
dic = {"k1": "v1v1", "k2": [11,22,33,44]}
PS:字典中的value只能是字符串或列表

8,写函数,此函数只接收一个参数且此参数必须是列表数据类型,此函数完成的功能是返回给调用者一个字典,此字典的键值对为此列表的索引及对应的元素。例如传入的列表为:[11,22,33] 返回的字典为 {0:11,1:22,2:33}。

9,写函数,函数接收四个参数分别是:姓名,性别,年龄,学历。用户通过输入这四个内容,然后将这四个内容传入到函数中,此函数接收到这四个内容,将内容追加到一个student_msg文件中。

10,对第9题升级:支持用户持续输入,Q或者q退出,性别默认为男,如果遇到女学生,则把性别输入女。

11,写函数,用户传入修改的文件名,与要修改的内容,执行函数,完成整个文件的批量修改操作(升级题)。

12,写一个函数完成三次登陆功能,再写一个函数完成注册功能(升级题)
注册:
1.要从文件中读取用户名和密码,匹配用户输入的用户名和文件中的用户名是否存在,如果存在,提示重新输入。
2.如果上面的判断没有问题,把用户名和密码写入到文件中。

2.题目详解

点击查看详细内容
  
2.
lst = (1,2,3,4,5)
def get_lst(x):
    new_lst = []
    for i in range(len(x)):
        if x[i]%2 !=0:
            new_lst.append(x[i])
    return new_lst
ret = get_lst(lst)
print(ret)


3.
def get_len(x):
    return len(x) > 5
print(get_len('123451'))


4.
def ret_lst(x):
    if len(x)>2:
        return x[:2]
    else:
        return '列表长度小于2'
lst = [133,2,1,23]
ret = ret_lst(lst)
print(ret)


5.
def get_sum(x):
    num = 0
    pha = 0
    space = 0
    other = 0
    for i in x:
        if i.isdigit():
            num +=1
        elif i.isalpha():
            pha +=1
        elif i.isspace():
            space +=1
        else:
            other +=1
    return '''
    数字:%d
    字母:%d
    空格:%d
    其他:%d
    ''' %(num,pha,space,other)
ret = get_sum('123asdfA  &*')
print(ret)


6.
def i_num(a,b):
    return a if a > b else b

ret = i_num(21,11)
print(ret)


7.
dic1 = {"k1": "v1v1", "k2": [11,22,33,44]}
def i_dic(dic):
    new_dic = {}
    for k,v in dic.items():
        if len(v)>2:
            s = v[0:2]      #保留的值内容
            new_dic[k] = s
        else:
            new_dic[k] = v
    return new_dic
print(i_dic(dic1))


8.
def r_dic(l):
    if type(l) == list:
        dic = {}
        for i in range(len(l)):
            dic[i] = l[i]
        return dic
    else:
        return '输入数据不是列表'
lst = [12,22,32]
ret = r_dic(lst)
print(ret)



9.
def i_msg(name,gender,age,edu):
    with open('student_msg','a+',encoding='utf-8') as f:
        f.write("%s_%s_%d_%s\n"%(name,gender,age,edu))

name = input("输入姓名:")
gender = input("输入性别:")
age = int(input("输入年龄:"))
edu = input("输入学历:")
i_msg(name,gender,age,edu)




10.
def i_msg(name,age,edu,gender='男'):
    with open('student_msg','a+',encoding='utf-8') as f:
        f.write("%s_%s_%d_%s\n"%(name,gender,age,edu))
while 1:
    content = input("是否录入学生信息(输入q/Q退出):")
    if content.upper() == 'Q':
        break
    else:
        name = input("输入姓名:")
        gender = input("输入性别:")
        age = int(input("输入年龄:"))
        edu = input("输入学历:")
        if gender == "":
            i_msg(name,age,edu)
        else:
            i_msg(name,age,edu,gender)



11.
import os
def r_file(filename,old,new):
    with open(filename,'r',encoding='utf-8') as f1,\
        open(filename+'副本','w',encoding='utf-8') as f2:
        for i in f1:
            i = i.replace(old,new)
            f2.write(i)
    os.remove(filename)
    os.rename(filename+'副本',filename)
r_file('test1.txt','123','321')




12.注意:文件的账户密码分隔符最好选一个用户用不到的
def enrol():
    while 1:
        username = input("注册用户名:").strip()
        password = input("注册密码:").strip()
        with open('user.txt','r+',encoding='utf-8') as f:
            for i in f:     #byh&&123
                lst = i.strip().split('&&')
                if username == lst[0]:
                    print('注册用户已存在,请重新输入')
                    break
                elif username == '' or password== '':
                    print('注册用户或密码不能为空,请重新输入')
                    break
            else:
                f.write('\n'+username+'&&'+password)
                break
    return "注册成功"

def login():
    count = 0
    while count < 3:
        i_username = input("登陆用户名:").strip()
        i_password = input('登陆密码:').strip()
        with open('user.txt','r',encoding='utf-8') as f:
            for i in f:
                lst = i.strip().split('&&')
                if i_username == lst[0] and i_password == lst[1]:
                    return "登陆成功"
            else:
                count += 1
                print('密码错误,可尝试次数【%s】'%(3-count))
    return "登陆失败"

def choose():
    while 1:
        ch = input("选择服务(1.注册/2.登陆/按q退出):")
        if ch == "1":
            print(enrol())
            break
        elif ch == "2":
            print(login())
            break
        elif ch.upper() == 'Q':
            break
        else:
            print("请输入正确的选项")

choose()
  

转载于:https://www.cnblogs.com/byho/p/10681817.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值