【发布时间】: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