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

①:写函数,接收n个数字,求这些参数数字的和

1 def sum_func(*args):2 sm =03 for i inargs:4 sm +=i5 returnsm6

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 inl:5 if n%2 == 1:6 li.append(i)7 n += 1

8 returnli9 print(jishu_list(l1))

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

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

1 deffunc(l):2 #return True if len(l) > 5 else False

3 return len(l) > 5 #比较运算本身返回bool值

4 print(func('546646'))#结果:True

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

1 deffunc(l):2 if len(l)>2:3 l = l[0:2]4 returnl5 else:6 returnl7 print(func([1,2,3,4]))8 print(func([1,2])) #结果:[1, 2]

#[1, 2]

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

1 s = 'das1 32 a2da'

2 deflei(l):3 num =04 isah =05 kong =06 other =07 for i inl :8 ifi.isdigit():9 num +=1

10 elifi.isalpha():11 isah +=1

12 elifi.isspace():13 kong +=1

14 else:15 other +=1

16 returnnum,isah,kong,other17 print(lei(s))

#结果:(4, 6, 3, 0)

⑥:写一个函数,判断用户传入的对象(字符串、列表、元组)的元素是否为空

1 deffunc(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 deflength_func(l):4 for k,v inl.items():5 if len(v) > 2:6 i = v[0:2]7 l[k]=i8 else:9 print('值长度小于2')10 returnl11 print(length_func(dic))

#结果:{'k1': 'v1', 'k2': [11, 22]}

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

1 defmy_max(a,b):2 returnmax(a,b)3

4 print(my_max(5,8))

#结果:8

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

方法一:直接写入文件

1 importos2

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 ini: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 importos2

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 ini:11 lis.append(new.strip())12 else:13 lis.append(i.strip())14 print(lis)15 for i inlis: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 =010 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 inf: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()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值