码迷,mamicode.com
首页 > 编程语言 > 详细

python的第九天

时间:2018-09-13 22:35:57      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:错误   false   []   参数   lower   成功   遇到   count   键值对   

‘‘‘2,写函数,检查获取传入列表或元组对象的所有奇数位索引对应的元素,并将其作为新列表返回给调用者。‘‘‘
def jishu(lis):
lis1 = []
for i in range(len(lis)):
if i % 2 == 1:
lis1.append(lis[i])
return lis1
lis = [1,2,3,4,5,6,7,8,9,10]
lis1 = jishu(lis)
print(lis1)
‘‘‘3,写函数,判断用户传入的对象(字符串、列表、元组)长度是否大于5。‘‘‘
def len_1(s):
if len(s) > 5:
return True
else:
return False
s = "46assd"
if len_1(s):
print("长度大于5")
else:
print("长度小于5")
‘‘‘4,写函数,检查传入列表的长度,如果大于2,将列表的前两项内容返回给调用者。‘‘‘
def len_2(s):
if len(s) > 2:
return [s[0], s[1]]
else:
return s
lis = [3,5,9,6,8]
lis1 = len_2(lis)
print(lis1)

def len_2(s):
if len(s) > 2:
if type(s) == str:
return s[:2]
elif type(s) == dict:
dic = {}
count = 0
for k, v in dict.items(s):
count += 1
dic.setdefault(k, v)
if count == 2:
return dic
else:
return s[0], s[1]
else:
return False
dic = [5,8,9,10]
s = len_2(dic)
print(s)
‘‘‘5,写函数,计算传入函数的字符串中, 数字、字母、空格 以及 其他内容的个数,并返回结果。‘‘‘
def count(s):
num = 0
letter = 0
kg = 0
qit = 0
for i in str(s):
if i.isdigit():
num += 1
elif i.islower() or i.isupper():
letter += 1
elif i == " ":
kg += 1
else:
qit += 1
return "数字:%s 字母:%s 空格:%s 其他内容:%s" % (num, letter, kg, qit)
st = "123 as d ‘‘‘"
stt = count(st)
print(stt)
‘‘‘6,写函数,接收两个数字参数,返回比较大的那个数字。‘‘‘
def up(a, b):
if type(a) == int and type(b) == int:
if a < b:
return b
else:
return a
else:
return False
a = up(10,8)
print(a)

‘‘‘7,写函数,检查传入字典的每一个value的长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者。‘‘‘
‘‘‘ dic = {"k1": "v1v1", "k2": [11,22,33,44]}
PS:字典中的value只能是字符串或列表
‘‘‘
def dic2(s):
dic1 = {}
for k, v in dict.items(s):
if type(v) == str and len(v) > 2:
dic1[k] = v[:2]
elif type(v) == list and len(v) > 2:
dic1[k] = [v[0], v[1]]
# print(v[0], v[1])
else:
dic1[k] = v
return dic1
dic = {"k1": "v1v1", "k2": [11,22,33,44]}
dic1 = dic2(dic)
print(dic1)

def dic2(s):
dic = {}
for k, v in dict.items(s):
if type(v) == int:
st = str(v)
dic.setdefault(k, int(st[:2]))
elif len(v) > 2:
if type(v) == str:
dic.setdefault(k, v[:2])
elif type(v) == dict:
count = 0
dic1 = {}
for k, v in dict.items(v):
count += 1
dic1.setdefault(k, v)
if count == 2:
break
dic.setdefault(k, dic1)
elif type(v) == list:
lis = [v[0], v[1]]
dic.setdefault(k, lis)
return dic
dic1 = {1:"asdas", 2:[1,2,3,4], 3:45644, 4:{3:3,4:5,6:7}}
ll = dic2(dic1)
print(ll)

‘‘‘
8,写函数,此函数只接收一个参数且此参数必须是列表数据类型,
此函数完成的功能是返回给调用者一个字典,
此字典的键值对为此列表的索引及对应的元素。
例如传入的列表为:[11,22,33] 返回的字典为 {0:11,1:22,2:33}。
‘‘‘
def dic(s):
dic1 = {}
for i in range(len(s)):
dic1.setdefault(i, s[i])
return dic1
lis = [1,2,3,4,4]
dic1 = dic(lis)
print(dic1)
‘‘‘
9,写函数,函数接收四个参数分别是:姓名,性别,年龄,学历。
用户通过输入这四个内容,然后将这四个内容传入到函数中,
此函数接收到这四个内容,将内容追加到一个student_msg文件中。
‘‘‘
def peop(name, sex, age, xue):
with open("student_msg", mode=‘a‘, encoding="utf-8") as f:
f.write("姓名:"+name+"\t")
f.write("性别:"+sex+"\t")
f.write("年龄:"+age+"\t")
f.write("学历:"+xue+"\t")
peop("张紫益", "男", "29", "中专")

‘‘‘10,对第9题升级:支持用户持续输入,Q或者q退出,性别默认为男,如果遇到女学生,则把性别输入女。‘‘‘
def peo(name, sex, xue, age="男"):
with open("student_msg", mode=‘a‘, encoding="utf-8") as f:
f.write("\n"+name+"\t"+sex+"\t"+age+"\t"+xue+"\t")
def pd():
pd = input("输入Q q 退出:").strip()
if pd.upper() == "Q":
return True
else:
return False

while 1:
name = input("请输入姓名:").strip()
age = input("请输入年龄:").strip()
xue = input("请输入学历:").strip()
sex = input("请输入性别:").strip()
if sex == "":
peo(name, age, xue)
if pd():
break
else:
peo(name, age, xue, sex)
if pd():
break
‘‘‘11,写函数,用户传入修改的文件名,与要修改的内容,执行函数,完成整个文件的批量修改操作(升级题)。‘‘‘
def gai(name, loor, new ):
import os
name1 = name+"a"
with open(name, mode="r", encoding="utf-8") as f1,\
open(name1, mode="w", encoding="utf-8") as f2:
for line in f1:
st = line.replace(loor, new)
f2.write(st)
os.remove(name)
os.rename(name1, name)
gai("student_msg","女","男")

‘‘‘12,写一个函数完成三次登陆功能,再写一个函数完成注册功能(升级题)‘‘‘
def zhuce():
print("请完成注册!")
name = input("用户名:").strip()
password = input("密码") .strip()
with open("xinxi", mode=‘a‘, encoding="utf-8")as f:
f.write(name+"\t"+password+"\n")
print("注册成功! 账号:%s 密码:%s" % (name, password))
def denglu():
count = 3
while count > 0:
count -= 1
name = input("请输入用户名:").strip()
password = input("请输入密码") .strip()
with open("xinxi", mode=‘r‘, encoding="utf-8") as f:
for line in f:
lis = line.split()
if lis[0] == name and lis[1] == password:
return True
else:
print("密码错误!您还有%s 次机会!" % count)
return False
while 1:
print("注册: 1 登录: 2 退出: Q")
num = input("请输入:").strip()
if num.upper() == "Q":
break
elif num == "1":
zhuce()
elif num == "2":
if denglu():
print("登录成功!")
break
else:
print("登录失败!")
else:
print("输入错误! 重新输入!")

python的第九天

标签:错误   false   []   参数   lower   成功   遇到   count   键值对   

原文地址:https://www.cnblogs.com/zhang-zi-yi/p/9643258.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!