”’
1、函数的定义:将可重复使用的,实现某种功能的代码段组织在一起
2、函数的语法:
def 函数名(参数):
函数体
return
2.1、函数名是一个变量,因此命名规则需遵守变量命名规则
3、函数的调用
函数名()
4、函数的返回值
return 可空、None、单个、多个以tuple返回给调用者
5、函数的参数:
函数定义时声明的变量的叫形参
1、位置参数
2、默认参数
函数调用时传给函数的叫实参
1、位置参数
定义多少个,调用时也必须传多少个
2、关键字参数
3、混合参数
位置参数需在关键字参数之前
”’
print('first'.center(40, '='))
def a3(arg):
ret = [ ]
for i in range(len(arg)):
if i % 2 == 1:
ret.append(arg[i])
else:
pass
return ret
li = [11,22,33,44,55]
r = a3(li)
print(li)
print(r)
print('first'.center(40, '='))
def fun1(n):
print('判断传入对象的长度是否大于5')
if len(n) >= 5:
return True
else:
return False
content = input('请输入内容:')
print(fun1(content))
dic = {"k1": "v1v1", "k2": [11,22,33,44]}
def a4(arg):
ret = {}
for key, value in arg.items():
if len(value) > 2:
ret[key] = value[0:2]
else:
ret[key] = value
return ret
r = a4(dic)
print(r)
def count(s):
dict = {'数字的个数:':0, '字母的个数:':0, '空格的个数:':0, '其他的个数:':0}
for i in s:
if i.isdigit():
dict['数字的个数:'] += 1
elif i.islower() or i.isupper():
dict['字母的个数:'] += 1
elif i.isspace():
dict['空格的个数:'] += 1
else:
dict['其他的个数:'] += 1
return dict
str = input('请输入内容:')
res = count(str)
print(res)
注汉字isalpha判断它为字母
print('first'.center(40, '='))
def num(a,b):
if a > b:
return a
else:
return b
r = num(1,2)
print(r)
dic = {"k1": "v1v1", "k2": [11,22,33,44]}
def a4(arg):
ret = {}
for key, value in arg.items():
if len(value) > 2:
ret[key] = value[0:2]
else:
ret[key] = value
return ret
r = a4(dic)
print(r)
def ret_dic(l):
dic = {}
if type(l) == list: # 判断类型,如果用is not 判断的是内存地址
for k, v in enumerate(l):
dic[k] = v
return dic
# 避免为空,return必须要返回的是个对象,故需要把return放带外面
# 如果传给的是空列表或其他类型的数据,不能返回None
def ret_dic2(lis):
dic = {}
if type(lis) == list:
for index in lis:
dic[index] = lis[index]
return dic # *******important
def input_file(name, sex, age, education):
with open(“student_msg”, “a”, encoding=”UTF-8”) as f:
li = “\t”.join([name, sex, age, education])
f.write(li+”\n”)
name = input(“请输入您的姓名:”)
sex = input(“请输入您的性别:”)
age = input(“请输入您的年龄:”)
education = input(“请输入您的学历:”)
input_file(name, sex, age, education)
def input_file(name,age, education,sex="男"):
with open(name, "a+", encoding= "UTF-8" ) as f:
s = "\t".join([name, sex, age, education])
f.write(s +"\n")
# nr=s +"\n"
# f.write(nr)
while 1:
name = input("请输入您的姓名(输入q或Q退出):")
if name.strip().upper() == "Q":
break
else:
sex = input("请输入您的性别(默认为男,回车即可):")
age = input("请输入您的年龄:")
education = input("请输入您的学历:")
if sex == "女":
input_file(name,age, education,sex )
else:
input_file(name,age, education)
print("输入成功")
break
def change_file(file_name, old_mesg, chang_mesg):
with open(“file_name”,”r”,encoding=”UTF-8”) as f1,\
open(“temp_file”,”w”,encoding=”UTF-8”) as f2:
for line in f1:
line = line.replace(old_mesg, chang_mesg)
f2.write(line)
import os
os.remove(file_name)
os.rename(“temp_file”, file_name)
user_name = 13321
password = 133211
def login():
for i in range(0,3):
name = int(input('请输入账号:'))
if name == user_name:
psw = int(input('请输入密码:'))
if psw == password:
print('登录成功'.center(40, '='))
return
else:
print('密码错误'.center(40, '='))
if i == 2:
print('您的机会已用完,请12小时之后再试')
else:
print('您还有%s次机会' % (2 - i))
else:
print('账号不存在'.center(40, '='))
if i == 2:
print('您的机会已用完,请12小时之后再试')
else:
print('您还有%s次机会' % (2 - i))
res = login()
def regist():
while 1:
print('请注册'.center(40, '='))
re_name = int(input('请输入账号:'))
if re_name == user_name:
print(''.center(40, '='))
else:
re_psw = int(input('请输入密码:'))
return '注册成功'.center(40, '=')
re = regist()
print(re)