【问题标题】:Python how to return a list to other functions?Python如何将列表返回给其他函数?
【发布时间】:2021-02-14 20:10:21
【问题描述】:

我一直在尝试返回列表,以便其他函数可以访问它。但在所有其他函数中,变量变得未定义。命令应该是“return twitchClipLinks”吧?

def api():
  #API via twitch to get the top clips of Just Chatting
  API_ENDPOINT = 'https://api.twitch.tv/kraken/clips/top?game=Just%20Chatting&period=day&trending=false&limit=6'
  ID = 'REMOVED'
  auth = 'application/vnd.twitchtv.v5+json'

  head = {
    'Client-ID' : ID,
    'Accept' : auth
  }

  r = requests.get(url = API_ENDPOINT, headers = head)

  twitchClipLinks = []
  data = r.json()

  for link in data['clips']:
    store = str(link['url'])
    twitchClipLinks.append(store)
  return twitchClipLinks

【问题讨论】:

  • 变量无法解决是什么意思?
  • 我得到了另外 5 个使用 twitchClipLinks 的 def。我收到错误未定义变量:'twitchClipLinks'
  • 你从那些函数中调用了这个函数吗?您是否将结果分配给任何变量?在每个函数中,您必须添加如下内容:twitchClipLinks = api() 或者您可以将 twitchClipLinks 定义为 global variable 并从您的 return 函数中删除 return 语句

标签: python python-3.x string list return


【解决方案1】:

在每个函数中,您必须添加如下内容:twitchClipLinks = api() 或者您可以将 twitchClipLinks 定义为 global variable 并从您的 api() 函数中删除 return 语句。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-13
    • 1970-01-01
    • 2019-12-18
    • 1970-01-01
    • 2020-06-10
    相关资源
    最近更新 更多