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.题目详解

点击查看详细内容

  1. 2.
  2. lst = [1,2,9,4,'a','b','c']
  3. def get_lst(x):
  4. n_lst = []
  5. for i in range(len(x)):
  6. if i%2 ==0:
  7. n_lst.append(x[i])
  8. return n_lst
  9. ret = get_lst(lst)
  10. print(ret)
  11. def get_len(x):

  12. return len(x) > 5

  13. print(get_len('123451'))
  14. def ret_lst(x):

  15. if len(x)>2:

  16. return x[:2]

  17. else:

  18. return '列表长度小于2'

  19. lst = [133,2,1,23]

  20. ret = ret_lst(lst)

  21. print(ret)
  22. def get_sum(x):

  23. num = 0

  24. pha = 0

  25. space = 0

  26. other = 0

  27. for i in x:

  28. if i.isdigit():

  29. num +=1

  30. elif i.isalpha():

  31. pha +=1

  32. elif i.isspace():

  33. space +=1

  34. else:

  35. other +=1

  36. return '''

  37. 数字:%d

  38. 字母:%d

  39. 空格:%d

  40. 其他:%d

  41. ''' %(num,pha,space,other)

  42. ret = get_sum('123asdfA &*')

  43. print(ret)
  44. def i_num(a,b):

  45. return a if a > b else b
  46. ret = i_num(21,11)

  47. print(ret)
  48. dic1 = {"k1": "v1v1", "k2": [11,22,33,44]}

  49. def i_dic(dic):

  50. new_dic = {}

  51. for k,v in dic.items():

  52. if len(v)>2:

  53. s = v[0:2] #保留的值内容

  54. new_dic[k] = s

  55. else:

  56. new_dic[k] = v

  57. return new_dic

  58. print(i_dic(dic1))
  59. def r_dic(l):

  60. if type(l) == list:

  61. dic = {}

  62. for i in range(len(l)):

  63. dic[i] = l[i]

  64. return dic

  65. else:

  66. return '输入数据不是列表'

  67. lst = [12,22,32]

  68. ret = r_dic(lst)

  69. print(ret)
  70. def i_msg(name,gender,age,edu):

  71. with open('student_msg','a+',encoding='utf-8') as f:

  72. f.write("%s_%s_%d_%s\n"%(name,gender,age,edu))
  73. name = input("输入姓名:")

  74. gender = input("输入性别:")

  75. age = int(input("输入年龄:"))

  76. edu = input("输入学历:")

  77. i_msg(name,gender,age,edu)
  78. def i_msg(name,age,edu,gender='男'):

  79. with open('student_msg','a+',encoding='utf-8') as f:

  80. f.write("%s_%s_%d_%s\n"%(name,gender,age,edu))

  81. while 1:

  82. content = input("是否录入学生信息(输入q/Q退出):")

  83. if content.upper() == 'Q':

  84. break

  85. else:

  86. name = input("输入姓名:")

  87. gender = input("输入性别:")

  88. age = int(input("输入年龄:"))

  89. edu = input("输入学历:")

  90. if gender == "":

  91. i_msg(name,age,edu)

  92. else:

  93. i_msg(name,age,edu,gender)
  94. import os

  95. def r_file(filename,old,new):

  96. with open(filename,'r',encoding='utf-8') as f1,

  97. open(filename+'副本','w',encoding='utf-8') as f2:

  98. for i in f1:

  99. i = i.replace(old,new)

  100. f2.write(i)

  101. os.remove(filename)

  102. os.rename(filename+'副本',filename)

  103. r_file('test1.txt','123','321')
  104. 12.注意:文件的账户密码分隔符最好选一个用户用不到的

  105. def enrol():

  106. while 1:

  107. username = input("注册用户名:").strip()

  108. password = input("注册密码:").strip()

  109. with open('user.txt','r+',encoding='utf-8') as f:

  110. for i in f: #byh&&123

  111. lst = i.strip().split('&&')

  112. if username == lst[0]:

  113. print('注册用户已存在,请重新输入')

  114. break

  115. elif username == '' or password== '':

  116. print('注册用户或密码不能为空,请重新输入')

  117. break

  118. else:

  119. f.write('\n'+username+'&&'+password)

  120. break

  121. return "注册成功"
  122. def login():

  123. count = 0

  124. while count < 3:

  125. i_username = input("登陆用户名:").strip()

  126. i_password = input('登陆密码:').strip()

  127. with open('user.txt','r',encoding='utf-8') as f:

  128. for i in f:

  129. lst = i.strip().split('&&')

  130. if i_username == lst[0] and i_password == lst[1]:

  131. return "登陆成功"

  132. else:

  133. count += 1

  134. print('密码错误,可尝试次数【%s】'%(3-count))

  135. return "登陆失败"
  136. def choose():

  137. while 1:

  138. ch = input("选择服务(1.注册/2.登陆/按q退出):")

  139. if ch == "1":

  140. print(enrol())

  141. break

  142. elif ch == "2":

  143. print(login())

  144. break

  145. elif ch.upper() == 'Q':

  146. break

  147. else:

  148. print("请输入正确的选项")
  149. choose()

day9函数作业详解的更多相关文章

  1. day10函数作业详解

    1.day10题目 2,写函数,接收n个数字,求这些参数数字的和.(动态传参) 3,读代码,回答:代码中,打印出来的值a,b,c分别是什么?为什么? a=10 b=20 def test5(a,b): ...

  2. day14内置函数作业详解

    day14题目 day14作业及默写 1,整理今天所学内容,整理知识点,整理博客. 2,画好流程图. 3,都完成的做一下作业(下面题都是用内置函数或者和匿名函数结合做出): 4,用map来处理字符串列 ...

  3. delphi中Application.MessageBox函数用法详解

    delphi中Application.MessageBox函数用法详解 Application.MessageBox是TApplication的成员函数,声明如下:functionTApplicati ...

  4. day22作业详解

    1.面向对象作业1 2.作业详解 点击查看详细内容 #1. class Li(object): def func1(self): print('in func1') obj = Li() obj.fu ...

  5. 自写函数VB6 STUFF函数 和 VB.net 2010 STUFF函数 详解

    '*************************************************************************'**模 块 名:自写函数VB6 STUFF函数 和 ...

  6. SQL Server数据库ROW_NUMBER()函数使用详解

    SQL Server数据库ROW_NUMBER()函数使用详解 摘自:http://database.51cto.com/art/201108/283399.htm SQL Server数据库ROW_ ...

  7. PHP函数篇详解十进制、二进制、八进制和十六进制转换函数说明

    PHP函数篇详解十进制.二进制.八进制和十六进制转换函数说明 作者: 字体:[增加 减小] 类型:转载   中文字符编码研究系列第一期,PHP函数篇详解十进制.二进制.八进制和十六进制互相转换函数说明 ...

  8. PHP date函数参数详解

    PHP date函数参数详解 作者: 字体:[增加 减小] 类型:转载       time()在PHP中是得到一个数字,这个数字表示从1970-01-01到现在共走了多少秒,很奇怪吧 不过这样方便计 ...

  9. SQL中CONVERT()函数用法详解

    SQL中CONVERT函数格式: CONVERT(data_type,expression[,style]) 参数说明: expression 是任何有效的 Microsoft® SQL Server ...

随机推荐

  1. 《CSS权威指南(第三版)》---第七章 基本视觉格式化

    主要知识记录: 1.给一个元素指定内容区宽度,如果设置了内边距,边框和外边距,这些因素都会影响CSS的width属性. 2.在水平格式化的7个属性中,width,margin-left,margin- ...

  2. 解决使用mybatis做批量操作时发生的异常:Parameter '__frch_item_0' not found. Available parameters are [list] 记录

    本文主要描述 使用mybatis进行批量更新.批量插入 过程中遇到的异常及总结: 首先贴出使用批量操作报的异常信息: java.lang.RuntimeException: org.mybatis.s ...

  3. 深入理解JVM - 虚拟机类加载机制 - 第七章

    类加载的时机类从被加载到虚拟机内存开始,到卸载出内存为止,它的整个生命周期包括了:加载/验证/准备/解析/初始化/使用/卸载七个阶段.其中验证/准备和解析统称为连接(Linking). 加载.验证.准 ...

  4. hdu 1002 A + B Problem II(大数)

    题意:就是求a+b (a,b都不超过1000位) 思路:用数组存储 第一道大数的题目,虽然很水,纪念一下! 代码: #include<cstdio> #include<cstring ...

  5. sphinx 全文搜索引擎

    sphinx的安装与配置 --------------------------------------------------------------------------------------- ...

  6. linux命令学习笔记(23):Linux 目录结构

    对于每一个Linux学习者来说,了解Linux文件系统的目录结构,是学好Linux的至关重要的一步.,深入了解linux文件 目录结构的标准和每个目录的详细功能,对于我们用好linux系统只管重要,下 ...

  7. js图片上传及显示

    html部分: <form action='' method='post' name='myform'> <input type='file' id='iptfileupload' ...

  8. BZOJ3674:可持久化并查集加强版

    浅谈主席树:https://www.cnblogs.com/AKMer/p/9956734.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem.p ...

  9. 51 nod 1522 上下序列——序列dp

    题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1522 很好的思想.考虑从小到大一对一对填数,这样也能对它的大小限制 ...

  10. Poj1007_DNA Sorting(面向对象方法)

    一.Description One measure of ``unsortedness'' in a sequence is the number of pairs of entries that a ...