首页 > 解决方案 > 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:

你好,

请忽略此电子邮件。

这是一条原始消息。

没有任何

标签: python

解决方案


  • 需要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() \

推荐阅读