python函数

函数

def sendmail(xo,zt,nr='ok'):          #默认函数 :默认函数一定要放在最后面

    try:

        import smtplib

        from email.mime.text import MIMEText

        from email.utils import formataddr

        msg = MIMEText('nr', 'plain', 'utf-8')

        msg['From'] = formataddr(["武沛齐", '13262209921@139.com'])

        msg['To'] = formataddr(["走人", 'xo'])

        msg['Subject'] = "zt"

        server = smtplib.SMTP("smtp.139.com", 25)

        server.login("13262209921@139.com", "520122817")

        server.sendmail('13262209921@139.com', ['xo', ], msg.as_string())

        server.quit()

    except:

        return "失败"

    else:

        return "cc"

#set = sendmail('13262209921@139.com','nihao',123456789)  普通函数

#set = sendmail('676989649@qq.com','nihao',123456789)

#sendmail(zt = 123456789, nr = 'nihao')                   指定函数

#  判断 成功好失败

while True:

    em = input('输入邮箱地址:')

    abc = sendmail(em,'shuaige','ok')                     普通函数

    if abc == "cc":

        print('发送成功')

    else:

        print('发送失败')

动态参数和万能参数

字符串

def fi(*args):

    print(args)

abc = 'ass',123

fi(*li)   加*把结果传进去 

fi(li)

列表

li = ['as', 123]

fi(*abc)

fi(abc)

def fl(**args):

    print(args, type(args))

字典

def fl(*abc,**args):

    print(abc,args)

dic = {'k1':'v1','k2':'v2'}

fl(**dic)

fl(n1='alex', n2=18)

格式化字符串1

a = ['hhh',456]

b = {'asd00':123,'name':'hanwei'}

 fl(*a,**b)

sl = 'i am {name},age {age}'.format(name='alex',age=18)

print(sl)

格式化字符串2 

dic = {'name':'alex','age':18}

s2 = 'i am {name},age {age}'.format(**dic)

print(s2)

1.函数引用变量是连接引用

2.全局变量

均可读

腻值 如 globalNAME

字典,列表 可修改 不可腻值

全局变量用大写

练习

def login(username,password):

#打开文件验证用户名和密码

#return为True 成功

#return为Fslse 失败

    f = open('db','r')

    for line in f:

        line_list = line.strip().split('|')

        if line_list[0] == username and line_list[1] == password:

            return True

    return False

def abc(usernam,passwor):

#把注册的用户和密码写入到文件

    with open('db','a')as w:

        wh = ' '+usernam+'|'+passwor

        w.write(wh)

        w.close()

        return True

def main():

#判读登录和注册

#把用户和密码传给函数

#判断还是否成功

    t = input ('1.登录 2.注册:')

    if t == '1':

        user = input('输入用户名:')

        pwd = input('输入密码:')

        r = login(user,pwd)

        if r == True:

            print('登录成功')

        else:

            print ('登录失败')

    elif t == '2':

        user1 = input('输入用户名:')

        pwd2 = input('输入密码:')

        M = len(pwd2)

        if M > 8:

            abc(user1,pwd2)

            print('成功注册%s' %user1)

        else:

             print('密码太短')

main()

原文地址:https://www.cnblogs.com/hanwei999/p/6018959.html