python - Python - 如何在另一个函数中使用函数
问题描述
我正在尝试在发送电子邮件时进行可变签名。根据登录操作系统的用户,这将显示不同的签名。尝试执行此操作时,我收到以下错误消息。
我尝试了 2 个我知道的选项,但无济于事。请有人能说明我如何让一个函数在另一个函数中工作,或者对此提出另一个建议:
示例 1:
def signature():
# User Variable(s)
user = os.getlogin()
name1 = "example1"
name2 = "example2"
name3 = "example3"
# Signature Variable(s)
if user == name1:
print("Kind Regards\n"
"Example 1\n"
"Job Role")
elif user == name2:
print("Kind Regards\n"
"Example 2\n"
"Job Role")
elif user == name3:
print("Kind Regards\n"
"Example 3\n"
"Job Role")
else:
print("Kind Regards\n"
"Other\n"
"Job Role")
def mail_send():
outlook = win32.Dispatch("outlook.application")
mail = outlook.CreateItem(0)
mail.To = "example@outlook.co.uk"
mail.Subject = "Python Test Mail"
mail.Body = "Hi," \
"\n\nPlease ignore this email." \
"\n\nThis is an original message." \
"\n" + (str(signature)) \
已发送邮件的输出:
你好,
请忽略此电子邮件。
这是一条原始消息。
<0x000002B9F726A160 处的函数签名>
示例 2:
def signature():
# User Variable(s)
user = os.getlogin()
name1 = "example1"
name2 = "example2"
name3 = "example3"
# Signature Variable(s)
if user == name1:
print("Kind Regards\n"
"Example 1\n"
"Job Role")
elif user == name2:
print("Kind Regards\n"
"Example 2\n"
"Job Role")
elif user == name3:
print("Kind Regards\n"
"Example 3\n"
"Job Role")
else:
print("Kind Regards\n"
"Other\n"
"Job Role")
def mail_send():
outlook = win32.Dispatch("outlook.application")
mail = outlook.CreateItem(0)
mail.To = "example@outlook.co.uk"
mail.Subject = "Python Test Mail"
mail.Body = "Hi," \
"\n\nPlease ignore this email." \
"\n\nThis is an original message." \
"\n" + (str(signature())) \
已发送邮件的输出 2:
你好,
请忽略此电子邮件。
这是一条原始消息。
没有任何
解决方案
- 需要
return
从函数使创建的变量对调用函数可用 - 函数调用
myfunc()
不是myfunc
例如:
def signature():
# User Variable(s)
user = os.getlogin()
name1 = "example1"
name2 = "example2"
name3 = "example3"
# Signature Variable(s)
if user == name1:
sig_str = "Kind Regards\n"+
"Example 1\n"+
"Job Role"
elif user == name2:
sig_str = "Kind Regards\n"+
"Example 2\n"+
"Job Role"
elif user == name3:
sig_str = "Kind Regards\n"+
"Example 3\n"+
"Job Role"
else:
sig_str = "Kind Regards\n"+
"Other\n"+
"Job Role"
return sig_str
def mail_send():
outlook = win32.Dispatch("outlook.application")
mail = outlook.CreateItem(0)
mail.To = "example@outlook.co.uk"
mail.Subject = "Python Test Mail"
mail.Body = "Hi," \
"\n\nPlease ignore this email." \
"\n\nThis is an original message." \
"\n" + signature() \
推荐阅读
- javascript - 数据表和 jquery 不执行选择器按钮单击
- visual-studio-code - VScode中注释代码的括号突出显示
- python - 无法在 PyCharm 中安装任何软件包
- react-native - WKWebview cookie 问题?
- javascript - 在 Javascript 中将附加数据传递给事件侦听器
- javascript - 为什么我的承诺没有正确解决?
- sql-server - 数据库邮件注册表设置不会保持配置
- python - 有没有办法在烧瓶中使用 df.to_html() 将数据帧呈现为 HTML 后显示特定行?
- call - 收益调用返回未定义
- html - 如何将文本与居中的图像左侧对齐?