关于简单的python函数的一些小练习题

①:写函数,接收n个数字,求这些参数数字的和
1 def sum_func(*args):
2     sm = 0
3     for i in args:
4         sm += i
5     return sm
6 
7 print(sum_func(1,2,3,7,4,5,6))
#结果:28
 

②:找出传入的列表或元组的奇数位对应的元素,并返回一个新的列表

 1 l1 = [1,2,3,4,5,6,7]
 2 def jishu_list(l,li = []):
 3     n = 1
 4     for i in l:
 5         if n%2 == 1:
 6             li.append(i)
 7         n += 1
 8     return li
 9 print(jishu_list(l1))

#结果:[1, 3, 5, 7]

 ③:写一个函数,判断用户传入的对象(不可哈希)长度是否大于5

1 def func(l):
2     # return True if len(l) > 5 else False
3     return len(l) > 5 #比较运算本身返回bool值
4 print(func('546646'))

#结果:True

④:写一个函数,判断用户传入的列表长度是否大于2,如果大于2,只保留前两个,并将新内容返回给调用者

 
 1 def func(l):
 2     if len(l)>2:
 3         l = l[0:2]
 4         return l
 5     else:
 6         return l
 7 print(func([1,2,3,4]))
 8 print(func([1,2]))

#结果:[1, 2] # [1, 2]
 

⑤:写函数,统计字符串中有几个字母,几个数字,几个空格,几个其他字符,并返回结果

 
 1 s = ' das1 32 a2da'
 2 def lei(l):
 3     num = 0
 4     isah = 0
 5     kong = 0
 6     other = 0
 7     for i in l :
 8         if i.isdigit():
 9             num +=1
10         elif i.isalpha():
11             isah +=1
12         elif i.isspace():
13             kong +=1
14         else:
15             other +=1
16     return num,isah,kong,other
17 print(lei(s)) 

#结果:(4, 6, 3, 0)
⑥:写一个函数,判断用户传入的对象(字符串、列表、元组)的元素是否为空
 1 def func(l):
 2     if len(l) == 0:
 3         return '该对象为空'
 4     else:
 5         return '该对象不为空'
 6 
 7 print(func((1,2,3)))
 8 print(func([]))
 
#结果:该对象不为空
# 该对象为空
⑦:写函数,检查传入字典的每一个value长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者
 1 dic = {"k1":"v1v1","k2":[11,22,33,44]}
 2 
 3 def length_func(l):
 4     for k,v in l.items():
 5         if len(v) > 2:
 6             i = v[0:2]
 7             l[k]= i
 8         else:
 9             print('值长度小于2')
10     return l
11 print(length_func(dic))
 
 #结果:{'k1': 'v1', 'k2': [11, 22]}
 

⑧:写函数,接收两个数字参数,返回比较大的数字

1 def my_max(a,b):
2     return max(a,b)
3 
4 print(my_max(5,8))

#结果:8

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

方法一:直接写入文件

 
 1 import os
 2 
 3 def func(file ,old ='',new = ''):#修改文件内容
 4     file1 = open('swap','w',encoding='utf8')
 5     file1.close()
 6     with open(file,mode='r',encoding='utf-8') as f1, \
 7             open('swap', 'a', encoding='utf8')as f2:
 8                     for i in f1: #输出每行内容
 9                         if old in i:
10                             f2.seek(2)
11                             f2.write('{}\n'.format(new.strip()))
12                         else:
13                             f2.seek(2)
14                             f2.write('{}\n'.format(i.strip()))
15 
16     os.remove(file)
17     os.rename('swap',file)
18 func('sss',old='老李',new='老郭')
 
 #结果:修改前的文件内容:老王
 #                   老李
 #                   老陈
 #     修改后的文件内容:老王
 #                   老郭
 #                   老陈
 

方法二:先转存列表再写入文件

 1 import os
 2 
 3 def func(file ,old ='',new = ''):#修改文件内容
 4     lis = []
 5     file1 = open('swap','w',encoding='utf8')
 6     file1.close()
 7     with open(file,mode='r',encoding='utf-8') as f1, \
 8             open('swap', 'a', encoding='utf8')as f2:
 9                     for i in f1: #输出每行内容
10                         if old in i:
11                             lis.append(new.strip())
12                         else:
13                             lis.append(i.strip())
14                     print(lis)
15                     for i in lis:
16                         print(i.strip())
17                         f2.write('{}\n'.format(i))
18     os.remove(file)
19     os.rename('swap',file)
20 func('sss',old='老李',new='老郭')
 
 #结果同方法一

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

 
 1 def zc_func():#账号注册
 2     username = input('请输入您注册的用户名:').strip()
 3     password = input('请输入您注册的密码:').strip()
 4     with open('user_pwd_note','w',encoding='utf-8') as f:
 5         f.write('{}\n{}'.format(username,password))
 6     return '恭喜您注册成功!'
 7 
 8 def logo_func():#账号登录
 9     i = 0
10     lis = []
11     while i < 3:
12         uname = input('请输入您的用户名:').strip()
13         passwd = input('请输入密码:').strip()
14         with open('user_pwd_note','r',encoding='utf-8') as f:
15             for j in f:
16                 lis.append(j.strip())
17         if uname == lis[0] and passwd == lis[1]:
18             print('登陆成功!')
19             break
20         else:
21             print('您输入的账号或密码错误,请重新输入!')
22         i += 1
23 
24 zc_func()
25 logo_func()

猜你喜欢

转载自www.cnblogs.com/kindvampire/p/10296874.html