Python之路第三篇:Python基础(13)——函数普通参数

Posted 漫画

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python之路第三篇:Python基础(13)——函数普通参数相关的知识,希望对你有一定的参考价值。

# 普通参数:严格按照顺利,将实际参数赋值给形式参数

# def send(name):
# ...
# send("eric")


def sendmail(mail_addr): #第一步、创建函数 (mail_addr是形式参数)第三步、mail_addr = {str}‘[email protected]
try: # 这个功能是捕捉异常,目前还没学到。
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr

msg = MIMEText(‘邮件内容‘, ‘plain‘, ‘utf-8‘)
msg[‘From‘] = formataddr(["福田", [email protected]‘])
msg[‘To‘] = formataddr(["走人", [email protected]‘])
msg[‘Subject‘] = "主题"

server = smtplib.SMTP("smtp.163.com", 25)
server.login("[email protected]", "521why,.")
server.sendmail([email protected]‘, [mail_addr, ], msg.as_string()) #将收件人地址‘[email protected]‘换成一个变量
server.quit()
except: #只要try下面的代码,捕捉到异常,就会执行except里面的代码
#发送失败执行
# return False #以上代码执行失败,就返回一个False
return "no" #返回的也可以是字符串
else:
#发送成功执行
# return True #否则,返回一个True
return "yes" #return返回值给函数调用者sendmail,



# sendmail()
# ret = sendmail(‘[email protected]‘) #第二步、调用函数 (‘[email protected]‘是实际参数)
# ret = sendmail("[email protected]")
# print(ret)
# if ret == True: #通过判断ret是False还是True来显示发送状态
# # if ret == "cc": #也可以判断返回的是不是定义的字符串值
# print("发送成功")
# else:
# print("发送失败")

while True:
em = input("请输入邮箱地址:")
# sendmail(em)
result = sendmail(em) #em是参数传递的内容,可以是多个
# print(result)
if result == "yes":
print("发送成功")
else:
print("发送失败")

以上是关于Python之路第三篇:Python基础(13)——函数普通参数的主要内容,如果未能解决你的问题,请参考以下文章

Python之路第三篇:Python基础

Python之路第三篇:Python基础

Python之路第三篇:Python基础(15)——函数指定参数

Python之路第三篇:Python基础(11)——set集合

Python之路第三篇:Python基础(17)——函数动态参数

Python之路第三篇:Python基础(12)——函数