转载:https://www.cnblogs.com/derezzed/articles/8124178.html
必做题:
1、整理函数相关知识点,画思维导图,写博客
2、写函数,检查获取传入列表或元组对象的所有奇数位索引对应的元素,并将其作为新列表返回给调用者。
def fuc1(user_li):
new_li = []
for i in range(len(user_li)):
if i % 2 == 1:
new_li.append(user_li[i])
return new_li
print(fuc1([2,3,3,4,2,3]))
3、写函数,判断用户传入的对象(字符串、列表、元组)长度是否大于5。
# def a(user_send):
# if len(user_send) > 5:
# print ('yes!the length is great')
# else:
# print('no!try agin')
# a([2,3,32,1,1,6])
4、写函数,检查传入列表的长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者。
# def a(user_send):
# if len(user_send) > 2:
# return user_send[2:]
# else:
# return ("try agin")
# result =a('abcd')
# print(result)
# 5、写函数,计算传入字符串中【数字】、【字母】、【空格] 以及 【其他】的个数,并返回结果。
# def fuc4(user_str):
# digit = []
# alpha = []
# spacebar = []
# other =[]
# for i in user_str:
# if i.isdigit():
# digit.append(i)
# elif i.isalpha():
# alpha.append(i)
# elif i ==' ':
# spacebar.append(i)
# else:
# other.append(i)
# return len(digit),len(alpha),len(spacebar),len(other)
# a,b,c,d = fuc4('asdasf asdas 123123 ,.')
# print('数字:',a ,' 字母:',b,'空格: ',c,'其他:' ,d)
# 6、写函数,检查用户传入的对象(字符串、列表、元组)的每一个元素是否含有空内容,并返回结果。
# def fuc5(user_send):
# j = 0
# for i in user_send:
# if i == ' ':
# j += 1
# if j > 0:
# print('yes it has spacebar')
# else:
# print('no spacebar')
# fuc5('sfasfa asda asd')
# 7、写函数,检查传入字典的每一个value的长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者。
dic = {"k1": "v1v1", "k2": [11,22,33,44]}
# PS:字典中的value只能是字符串或列表
# def fuc6(user_key,user_value):
# if len(user_value) > 2:
# dic.setdefault(user_key,user_value[:2])
# return dic[user_key]
# print(fuc6('k3','sadasd'))
# 8、写函数,接收两个数字参数,返回比较大的那个数字。
# def fuc7(num1,num2):
# new_num = num1 if num1 > num2 else num2
# return new_num
# print(fuc7(20,32))
# 选做题:
# 9、写函数,用户传入修改的文件名,与要修改的内容,执行函数,完成整个文件的批量修改操作(进阶)。
def fuc8(file_name,file_content):
f = open(file_name,'w')
f.write(file_content)
f.close()
fuc8('new_file.txt','iron maiden')
fuc8('new_file.txt','xxxxxxxxxxxxxxxxxx')
# 10、写一个函数完成三次登陆功能,再写一个函数完成注册功能
# 明日内容:
# 函数的动态参数:http://www.cnblogs.com/Eva-J/articles/7125925.html
# 函数的进阶内容:http://www.cnblogs.com/Eva-J/articles/7156261.html
#
# 默写内容:
# def my_len(lst):
# count = 0
# for i in lst:
# count+=1
# return count
#
# l = [1,2,3,4]
# len_count = my_len(l)
# print(len_count)
#