【问题标题】:How to get the output of an Object function as Variable如何将对象函数的输出作为变量
【发布时间】:2019-11-10 19:26:43
【问题描述】:

我对此并不陌生,并尝试将此函数的输出作为字符串传递给 tweepy 更新方法(作为雕像发布)

              def hello():
                  name = str(input("Enter your name: "))
               if name:
                  print ("Hello " + str(name))
               else:
                 print("Hello World") 
                 return 

                 hello()

这里的问题是 Tweepy 的“api.update_status (status)”只接受字符串或包含字符串的变量。使用函数调用会打印出错误。

我该如何进行这个简单的过程?

我试图将函数输出作为变量传递

             test = hello()

但这会将错误打印到 twitter。

              ...
              api.update_status (test)

【问题讨论】:

  • 我比较关心函数调用的输出。如何通过 tweepy 在 Twitter 上发布此内容?
  • 您必须使用return "Hello World" 而不是print("Hello World")。与其他print() 相同
  • 总是将完整的错误消息(从单词“Traceback”开始)作为文本(不是屏幕截图)放在有问题的(不是评论)中。还有其他有用的信息。
  • input() 总是给出字符串,所以你不需要str()

标签: python string function python-3.6


【解决方案1】:

你的函数返回一个 None 对象,你的函数应该返回一个字符串 上面提到的@furas。

试试这个:

def hello():
    name = input("Enter your name: ")
    if name:
        return "Hello " + name
    else:
        return "Hello World"


hello()

你仍然将函数调用传递给 api

api.update_status(hello())

【讨论】:

    猜你喜欢
    • 2020-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-26
    • 2020-08-10
    • 2013-01-02
    • 1970-01-01
    相关资源
    最近更新 更多