9.2 Job

A three way function definition outlined

1. Empty function

def register():
    #注册功能,暂时不会写
    pass #写个空函数占位置

2. Reference function

def compare():
    if x>y:
        print(x)
    else:
        print(y)
compare(30,40)   #打印40

3. No reference function

def  func():
    print(1)
func()

Second, the return value of the function outlined

1. If the function does not return a value, the default return None

None is the return value of the function is used to get a result

def compare():
    print(1,compare)  #<function compare at 0x000000000581C620> 函数内存地址
    res=compare()  #res接收一个变量值,函数的返回值
    print('res:',res)
compare()

2. The function returns the value returned by the return

def compare():
   x=1
   y=2
   if x>y:
   return x
   else:
   return y
res=compare()
print(res)

3.return may terminate function

def print_1_10():
   for i in range(10):
       if i==5:
       return  #终止函数
   print(i)
print_1_10()      

4.return can return multiple values ​​are stored in list form

def return_nums():
    return 10,100,1000
res=return_nums()
print(res[2])  #1000

Three parameters, functions outlined

1. formal and actual parameters

def compare(x,y):  #形参,一般具有描述的意义,毫无作用(接收实参)--》相当于变量名,接收变量值
    if x>y:
        print(x)
    else:
        print(y)
compare(30,40)  #实参,具有实际的意义,具体的一个值(传给形参)--》相当于变量值

2. Location parameter: a write from left to right a past, called the location parameter

3. The default parameter: location parameter to a default value, he becomes the default parameter, it does not need to call after the deposit value, but if it will keep the value of the reception.

The default parameter have to be placed behind the position parameter

4. Keyword argument: position of the real participation on the default value, and according to the location name to the value of the parameter, the parameter generally used in excess (function parameters recommended no more than 3)

Keyword argument must also be in the back position parameters

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

def compare(a,b,c,d):
   print(a,b,c,d)
   res1=compare2(a,b)
   res2=compare2(c,d)
   res3=compare2(res1,res2)
   return res3
res=compare(4,0,d=2,c=1)
print(res)  #4,0,1,2 最大4

Fourth, write register function

def register():
    print('注册功能')
    usename_inp=input('请输入你的用户名:')
    print('usename_inp:',usename_inp)
    pwd_inp=input('请输入你的密码:')
    print('pwd_inp:',pwd_inp)
    with open('user_info.txt','a',encoding='utf8') as fa:
         if ':'in usename_inp:
             print('输入错误,用户名不能包含:')
         else:
             fa.write(f'{usename_inp}:{pwd_inp}')
             print('保存成功')
register()

V. writing a login function

def login():
    print('登录功能')
    with open('user_info.txt','r',encoding='utf8')as fr:
        data=fr.read()
        data_split=data.split(':')

    usename,pwd=data_split[0],data_split[1]   #一次性定义多个变量

    usename_inp = input('请输入你的用户名:')
    pwd_inp = input('请输入你的密码:')
    if usename_inp==usename and pwd_inp==pwd:
        print('登录成功')
    else:
        print('登录失败')
login()

Guess you like

Origin www.cnblogs.com/lidandanaa/p/11447783.html