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,9,4,'a','b','c']
def get_lst(x):
    n_lst = []
    for i in range(len(x)):
        if i%2 ==0:
            n_lst.append(x[i])
    return n_lst
ret = get_lst(lst)
print(ret)

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

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)

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)

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

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

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))

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)

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

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

def i_msg(name,age,edu,gender='男'):
with open('student_msg','a+',encoding='utf-8') as f:
f.write("%s_%s_%d_%s "%(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)

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(' '+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