What is the function
Simply speaking, the function is the process that is "derived from the input and output of the process." It can also be seen as a tool to accomplish a specific task. When we often need to complete this particular task, we will be able to complete this task after the whole process standardized packaged as a tool. Directly call when needed, simply enter the required parameters when you call, you can quickly get the desired output.
Function definition:
Defined registration function
def register():
username_inp = input('请输入你的用户名: ')
print('username_inp: ',username_inp)
pwd_inp = input('请输入你的密码: ')
print('pwd_inp: ',pwd_inp)
with open('username_info.txt','a',encoding = 'utf8') as fa:
if':' in username_inp:
print('输入错误,用户名不能含有冒号')
else:
fa.write(f'{username_inp}:{pwd_inp}')
print('保存成功')
Define the login function
def login():
with open('username_info.txt','r',encoding='utf8') as fr:
data = fr.read()
data_split = data.split(':')
username,pwd = data_split[0],data_split[1]
username_inp = input('请输入你的用户名: ')
pwd_inp = input('请输入你的密码: ')
if username == username_inp and pwd == pwd_inp:
print('登录成功')
else:
print('登录失败')
register()
login()
Three definitions form of the function
# 1、空函数
def register():
TODO # 先站住位置,后期根据需要补充
pass
# 2、带参函数
# 2.1 参数固定
def compare():
x = 10 # 参数固定的函数
y = 20 # 参数固定的函数
if x > y:
print(x)
else:
print(y)
compare() # 调用函数
# 运行结果显示为:20
# 2.2 参数不固定
def compare(x,y): # 参数没固定,需要输入参数的函数,在括号中声明
if x > y:
print(x)
else:
print(y)
compare(12,23) # 调用函数
# 运行结果显示为:23
# 3、无参函数
def func():
print(1)
func() # 调用函数
# 运行结果显示为:1
Function's return value
def add(x,y): # 定义一个函数
print(x+y)
add(1,2) # 调用函数
# 当我们不需要打印函数计算结果,只需要调用这个结果时,就只需用return返回这个结果
def add(x, y): # 定义一个函数
return x+y # 指定返回函数结果
add(1,2) # 调用函数后,没有显示结果值,但结果值在内存中待用,可以赋给变量名
res = add(1,2) # 返回的结果值赋给res
print(res) # 打印res显示: 3
def add(x, y): # 定义一个函数
print(x+y)
return x+y # 指定返回函数结果
print(9)
res = add(1,2) # 返回的结果值赋给res
print(res)
# 运行结果显示为:3 3
# 没有打印 9 ,因为函数中的return有结束函数的作用,所以return之后的代码不会被执行,此处print(9)不会被执行
def add(x, y): # 定义一个函数
return (x,y,x+y)
res = add(1,2)
print(res)
# # 运行结果显示为:(1, 2, 3),因为return可以返回任意数据类型
def add(x, y): # 定义一个函数
return x,y,x+y
res = add(1,2)
print(res)
# 运行结果显示为:(1, 2, 3),因为return不加括号返回多个值时,默认以元组的数据形式返回
def add(x, y):
return x+y
res = add(1,2)
print(res*5) # 调用函数后得到的返回值可以用于后续使用
# 运行结果显示为:15
Function parameters
def compare(x,y): # x,y皆为形参,相当于变量名,具有描述意义,接受赋值
if x > y:
print(x)
else:
print(y)
compare(2,3)
# 2和3为实参,实际用于运算的参数,相当于变量值,此处用于给形参赋值
# 位置形参-->位置实参,从左到右一一对应赋值,不可多不可少,
# 默认形参:给位置形参一个默认值,即成为默认形参,调用函数时不必给其传值,有需要也可给其传值
def compare(x,y=3): # x,y皆为形参,相当于变量名,具有描述意义,接受赋值
if x > y:
print(x)
else:
print(y)
compare(2) # 此时2是传给x的,运行结果显示为:3
compare(2,1) # 需要改变默认形参y的值时,可以把新的值传1给他,运行结果显示为:2
def compare1(num1,num2):
if num1 > num2:
return num1
else:
return num2
def compare(x1,x2,x3,x4):
res1 = compare1(x1,x2)
res2 = compare1(x3,x4)
res3 = compare1(res1,res2)
return res3
res = compare(2,5,6,9)
print(res)
# 打印结果显示为:9
# 当参数过多时,我们可以按照位置形参名赋值,也可进行关键字参数赋值,如下所示:
res = compare(2,5,x4=9,x3=6) # 关键字参数和默认参数必须在位置形参后边
print(res)
# 打印结果显示为:9
# 定义函数的参数时,最好是规定清楚需要输入的的参数类型和返回值的数据类型
def f1(num:list, target:int) -> int:
print(num,target)
return 1
f1([1,2],3) # 输入参数时,注意格式符合函数定义的参数格式
operation:
4, the preparation of the registration function
The user information stored in the file, the user information can be saved asnick:123|sean:456|tank:789
# 定义注册函数
def register():
username_inp= input('请输入你的用户名: ')
if ':' in username_inp:
print('输入错误,用户名不能含有冒号')
# print('username_inp: ',username_inp)
pwd_inp = input('请输入你的密码: ')
# print('pwd_inp: ',pwd_inp)
with open('usernames_info.txt','a',encoding = 'utf8') as fa:
fa.write(f'{username_inp}:{pwd_inp},')
print('保存成功')
register()
5, writing a login function
User information is read from the file identification
# 定义登录函数
def login():
with open('usernames_info.txt','r',encoding='utf8') as fr:
data = fr.read()
data_split = data.split(',')
dic = {}
for i in data_split[0:-1]:
i_split = i.split(':')
dic[i_split[0]] = i_split[1]
username_inp = input('请输入你的用户名: ')
pwd_inp = input('请输入你的密码: ')
if username_inp in dic and dic[username_inp] == pwd_inp:
print('登录成功')
else:
print('登录失败')
login()