2、写函数,检查获取传入列表或元组对象的所有奇数位索引对应的元素,并将其作为新列表返回给调用者。

def dan_index(itter):
li=[]
for i in range(len(itter)):
if i%2==1:
li.append(itter[i])
return li

3、写函数,判断用户传入的对象(字符串、列表、元组)长度是否大于5。 

def len_itter(itt):
count=0
for i in itt:
count+=1
if count>5:
return "{}长度大于5".format(itt)
else:
return "%s 长度小于5"%itt
print(len_itter([3489,34,"hello","myfu","good",78]))

4、写函数,检查传入列表的长度,如果大于2,将列表的前两项内容返回给调用者。 

def it_len(itt):
if len(itt)>2:
return itt[:2]

5、写函数,计算传入函数的字符串中, 数字、字母、空格 以及 其他内容的个数,并返回结果。

def str_count(st):
s=0
b=0
n=0
for i in st:
if i.isdigit():
n+=1
if i.isalpha():
s+=1
if i.isspace():
b+=1
return "%s有%s个数字%s字母%s空格"%(st,n,s,b)

6、写函数,接收两个数字参数,返回比较大的那个数字。 

def num_cam(a,b):
if a>b:
return a
else:
return b

7、写函数,检查传入字典的每一个value的长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者。

dic = {"k1": "v1v1", "k2": [11,22,33,44]}

PS:字典中的value只能是字符串或列表

def dic_filter(dic):
for i,j in dic.items():
if len(j)>2:
j=j[:2]
dic.update({i:j})
return dic
print(dic_filter( {"k1": "v1v1", "k2": [11,22,33,44]}))

8、写函数,此函数只接收一个参数且此参数必须是列表数据类型,此函数完成的功能是返回给调用者一个字典,此字典的键值对为此列表的索引及对应的元素。例如传入的列表为:[11,22,33] 返回的字典为 {0:11,1:22,2:33}。

#方法一
def list_trans(li):
dic={}
for i,j in enumerate(li):
dic.setdefault(i,j)
return dic
print(list_trans([11,22,33]))
#方法二
def list_trans(li):
dic={}
for i in range(len(li)):
dic.setdefault(i,li[i])
return dic
print(list_trans([11,22,33]))

9、写函数,函数接收四个参数分别是:姓名,性别,年龄,学历。用户通过输入这四个内容,然后将这四个内容传入到函数中,此函数接收到这四个内容,将内容追加到一个student_msg文件中。

def stu_add(name,gender,age,education):
with open("stu_info.txt","r+",encoding="utf-8") as f2:
data=f2.readline()
f2.write("\n%s %s %s %s"%(name,gender,age,education))

 10、对第9题升级:支持用户持续输入,Q或者q退出,性别默认为男,如果遇到女学生,则把性别输入女。

def stu_add():
with open ("stu_info.txt","r+",encoding="utf-8") as f1:
while True:
stu_add=input("请输入用户信息(以空格隔开q或Q退出):").strip()
if len(stu_add.split())==3:
name,age,education=stu_add.split()
f1.read()
f1.write("\n%s male %s %s"%(name,age,education))
continue
if len(stu_add.split())==4:
name,gender,age,education=stu_add.split()
f1.read()
f1.write("\n%s %s %s %s"%(name,gender,age,education))
continue
if stu_add.upper()=="Q":
exit("谢谢使用,正在退出")

11、写函数,用户传入修改的文件名,与要修改的内容,执行函数,完成整个文件的批量修改操作(升级题)。

def file_modify(filename,contant):
import os
contant_new = input("请输入修改后的内容:")
with open(filename,encoding="utf-8")as f1,open("filename_new","w",encoding="utf-8") as f2:
for i in f1:
new_i=i.replace(contant,contant_new)
f2.write(new_i)
os.remove(filename)
os.rename("filename_new",filename)

12、写一个函数完成三次登陆功能,再写一个函数完成注册功能(升级题)

def user_register():
while True:
uname=input("请输入注册名:")
pwd=input("请输入注册密码:")
pwd_2=input("请确认密码:")
li=[]
with open("info_database","r+",encoding="utf-8") as f1:
data=f1.read()
data_dic=eval(data)
for i in data_dic.keys():
li.append(i)
if uname in li:
print("用户名已存在,请重新输入")
continue
if pwd==pwd_2:
print("注册成功")
data_dic.setdefault(uname,pwd)
f1.seek(0)
f1.write(str(data_dic))
break
else:
print("密码不一致请重新输入")
continue
def login():
count=0
while count<3:
username=input("请输入用户名:").strip()
password=input("请输入密码:").strip()
with open("info_database",encoding="utf-8")as f2:
data=f2.read()
dic_data=eval(data)
for i in dic_data.keys():
if username==i and password==dic_data[i]:
print("登录成功")
exit()
else:
print("用户名或密码有误还有%s次机会"%(2-count))
count+=1

python练习题-day9的更多相关文章

  1. Python练习题 028:求3*3矩阵对角线数字之和

    [Python练习题 028] 求一个3*3矩阵对角线元素之和 ----------------------------------------------------- 这题解倒是解出来了,但总觉得 ...

  2. Python练习题 027:对10个数字进行排序

    [Python练习题 027] 对10个数字进行排序 --------------------------------------------- 这题没什么好说的,用 str.split(' ') 获 ...

  3. Python练习题 026:求100以内的素数

    [Python练习题 026] 求100以内的素数. ------------------------------------------------- 奇怪,求解素数的题,之前不是做过了吗?难道是想 ...

  4. Python练习题 025:判断回文数

    [Python练习题 025] 一个5位数,判断它是不是回文数.即12321是回文数,个位与万位相同,十位与千位相同. ---------------------------------------- ...

  5. Python练习题 024:求位数及逆序打印

    [Python练习题 024] 给一个不多于5位的正整数,要求:一.求它是几位数,二.逆序打印出各位数字. ---------------------------------------------- ...

  6. Python练习题 004:判断某日期是该年的第几天

    [Python练习题 004]输入某年某月某日,判断这一天是这一年的第几天? ---------------------------------------------- 这题竟然写了 28 行代码! ...

  7. Python练习题-1.使用匿名函数对1~1000求和,代码力求简洁。

    Python 练习 标签(空格分隔): Python Python练习题 Python知识点 一.使用匿名函数对1~1000求和,代码力求简洁. 答案: In [1]: from functools ...

  8. PYTHON练习题 二. 使用random中的randint函数随机生成一个1~100之间的预设整数让用户键盘输入所猜的数。

    Python 练习 标签: Python Python练习题 Python知识点 二. 使用random中的randint函数随机生成一个1~100之间的预设整数让用户键盘输入所猜的数,如果大于预设的 ...

  9. python 基础 2.8 python练习题

    python 练习题:   #/usr/bin/python #coding=utf-8 #@Time   :2017/10/26 9:38 #@Auther :liuzhenchuan #@File ...

随机推荐

  1. CentOS 7.4nginx配置SSL

    一.在/etc/nginx/conf.d目录下创建虚拟主机配置文件 server { listen 80; server_name www.xx.com xx.com; return 301 http ...

  2. go 源码学习之---Tail 源码分析

    已经有两个月没有写博客了,也有好几个月没有看go相关的内容了,由于工作原因最近在做java以及大数据相关的内容,导致最近工作较忙,博客停止了更新,正好想捡起之前go的东西,所以找了一个源码学习 这个也 ...

  3. 移动开发常用meta设置

    <!-- 视图窗口,移动端特属的标签. --> <meta name="viewport" content="width=device-width,in ...

  4. 推荐使用@Resource,不推荐使用@Autowired

    @Autowired 默认根据ByType, 当一个类有两个对象的时候,会报错. @Resource 默认是ByName,可以精准的找到<bean>的配置项. jar包推送,应该级联推送: ...

  5. 大数据架构:搭建CDH5.5.1分布式集群环境

    yum install -y ntp gcc make lrzsz wget vim sysstat.x86_64 xinetd screen expect rsync bind-utils ioto ...

  6. [译] 理解 LSTM(Long Short-Term Memory, LSTM) 网络

    本文译自 Christopher Olah 的博文 Recurrent Neural Networks 人类并不是每时每刻都从一片空白的大脑开始他们的思考.在你阅读这篇文章时候,你都是基于自己已经拥有 ...

  7. Houdini技术体系 基础管线(一) : Houdini与Houdini Engine的安装

    Houdini 下载与安装 在官网 https://www.sidefx.com/download/ 下载最新的Production Build 版本,当前是16.5版本,需要注册帐号 PS:公司内网 ...

  8. 复制id_rsa命令

    pbcopy < ~/.ssh/id_rsa.pub https://aliasan-conf.taijiankong.cn/duotai/2T7b253i8.pac

  9. Jquery实现轮播公告

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. Vivado Design Suite用户指南之约束的使用第一部分(介绍部分)

    首先来看目录部分: 首先是介绍部分:这部分讲述的是Migrating From UCF Constraints to XDC Constraints(从UCF约束迁移到XDC约束)和About XDC ...