PT--函数 返回值 参数
后面 购物车重点等
1 # 1. 整理博客 2 3 # 2.写函数,检查获取传入列表或元组对象的所有奇数位索引对应的元素, 4 # 并将其作为新列表返回给调用者。 5 def check(s): 6 return(s[::2]) #取奇数位 7 li=['ji','koa','wyuq','i9','876'] 8 print(check(li)) 9 10 # 3.写函数,判断用户传入的对象(字符串、列表、元组)长度是否大于5。 11 def length(s): #s为列表 字符串 元组 12 if len(s)>5: 13 print('长度大于5') 14 else: 15 print('长度不大于5') 16 a=['pol','wer','hjk','mnbh'] 17 length(a) 18 19 #方法2: 20 def func(a): 21 return len(a)>5 22 print(func('ijold')) 23 24 25 # 4.写函数,检查传入列表的长度,如果大于2,将列表的前两项内容返回给调用者。 26 def check(s): #s为列表 27 if len(s)>2: 28 return s[0:2] 29 a=['iui',1,'owe','poi',3,4] 30 print(check(a)) 31 32 # 5.写函数,计算传入函数的字符串中, 数字、字母、空格 以及其他内容的个数,并返回结果 33 def count(s): #s为字符串 34 dic={'n':0,'f':0,'k':0,'q':0} 35 for i in s: 36 if i.isdigit(): 37 dic['n']+=1 38 elif i.isalpha(): #汉字属于alpha 是个坑 39 dic['f']+=1 40 elif i==' ': 41 dic['k']+=1 42 else: 43 dic['q']+=1 44 return dic 45 s='jjdi7874 &&(%huhdu%^%8938' 46 print(count(s)) 47 48 49 # 6. 接收两个数字参数,返回比较大的那个数字。 50 def compare(a,b): 51 if a>b: 52 return a 53 else: 54 return b 55 print(compare(34,89)) 56 57 58 #方法二: 59 c=a if a>b else b #如果a>b 返回前面的,否则返回后面的 60 61 62 # 7.写函数,检查传入字典的每一个value的长度,如果大于2, 63 # 那么仅保留前两个长度的内容,并将新内容返回给调用者。 64 # dic = {"k1": "v1v1", "k2": [11,22,33,44]} 65 # PS:字典中的value只能是字符串或列表 66 67 def get_xin(dic): 68 for i in dic: #key 69 if len(dic[i])>2: 70 dic[i]=dic[i][0:2] 71 return dic 72 dic = {"k1": "v1v1", "k2": [11,22,33,44]} 73 print(get_xin(dic)) 74 75 76 # 8.写函数,此函数只接收一个参数且此参数必须是列表数据类型, 77 # 此函数完成的功能是返回给调用者一个字典, 78 # 此字典的键值对为此列表的索引及对应的元素。 79 # 例如传入的列表为:[11,22,33] 返回的字典为 {0:11,1:22,2:33}。 80 81 def change(lis): 82 if type(lis) == list: #判断是否为列表类型 83 dic={} 84 for el in lis: 85 dic[lis.index(el)]=el 86 return dic 87 else: 88 return '不是列表' 89 li=[11,22,33] 90 print(change(li)) 91 92 93 # 9.写函数,函数接收四个参数分别是:姓名,性别,年龄,学历。 94 # 用户通过输入这四个内容,然后将这四个内容传入到函数中, 95 96 # 此函数接收到这四个内容,将内容追加到一个student_msg文件中。 97 def add(name,gender,age,education): 98 f=open('student_msg',mode='a',encoding='utf-8') 99 f.write('%s,%s,%s,%s\n'%(name,gender,age,education)) 100 f.flush() 101 f.close() 102 name=input('请输入姓名:') 103 gender=input('请输入性别:') 104 age=input('请输入年龄:') 105 education=input('请输入学历:') 106 add(name,gender,age,education) 107 108 109 110 # 10.对第9题升级:支持用户持续输入,Q或者q退出, 111 # 性别默认为男,如果遇到女学生,则把性别输入女。 112 113 def add(name,age,education,gender='男'): 114 f=open('student_msg',mode='a',encoding='utf-8') 115 f.write('%s,%s,%s,%s\n'%(name,age,education,gender)) 116 f.flush() 117 f.close() 118 return 119 while 1: 120 a=input('是否录入((Q或q退出))') 121 if a.upper()=='Q': 122 break 123 else: 124 name=input('请输入姓名:') 125 age=input('请输入年龄:') 126 education=input('请输入学历:') 127 gender=input('请输入性别:') 128 if gender == '': #空不能用None表示,要用空字符串表示 129 add(name,age,education) #第四个参数不存在则默认为男 130 else: 131 add(name,age,education,gender) 132 133 134 # 11.写函数,用户传入修改的文件名,与要修改的内容,执行函数, 135 # 完成整个文件的批量修改操作(升级题)。 136 137 import os 138 def modify(filename,content1,content2): 139 with open(filename,mode='r',encoding='utf-8') as f1,\ 140 open(filename+'_副本',mode='w',encoding='utf-8') as f2: 141 for line in f1: 142 line=line.replace(content1,content2) 143 f2.write(line) 144 os.remove(filename) # 注意缩进 145 os.rename(filename+'_副本',filename) 146 modify('a1.txt','alex','sb')
1 # 12.写一个函数完成三次登陆功能,再写一个函数完成注册功能(升级题) 2 def zhuce(name,key): 3 username=name 4 password=key 5 f=open('a1.txt',mode='a+',encoding='utf-8') 6 f.seek(0) 7 li=f.readlines() 8 for el in li: 9 ls=el.strip().split(':') 10 if username==ls[0]: 11 print('用户名已存在,请重新注册') 12 break 13 else: 14 f.write('\n'+username+':'+password) #换行要加冒号 15 print('注册成功') 16 f.flush() 17 f.close() 18 return 19 def denglu(): 20 for i in range(3): 21 my_username=input('请输入用户名:') 22 my_password=input('请输入密码:') 23 f = open('a1.txt', mode='r', encoding='utf-8') 24 li=f.readlines() 25 for el in li: 26 ls=el.strip().split(':') 27 if my_username==ls[0] and my_password==ls[1]: 28 print('登录成功!') 29 return 30 else: 31 print('登录失败,请重新登录!') 32 while 1: 33 content=input('请选择登录或注册(q或Q退出):') 34 if content.upper()=='Q': 35 break 36 elif content=='登录': 37 denglu() 38 elif content=='注册': 39 my_user=input('请输入用户名;') 40 my_word=input('请输入密码:') 41 zhuce(my_user,my_word) 42 else: 43 continue

浙公网安备 33010602011771号