[英]Why is my code not appending these results?
我正在尝试通过编写一些返回单词的代码来学习如何使用 api,这些代码可以从您为一个名为 wordscape 的游戏提供的一些字母中生成。
代码运行没有错误,但我很困惑为什么它只附加了一些正确的结果,而不是全部。
这是问题的一个示例,使用代码片段:
import requests
test=["we","ew"]
for word in test:
parameters={"define":word}
r=requests.get("https://googledictionaryapi.eu-gb.mybluemix.net",params=parameters)
liste=[]
if r.status_code==200:
liste.append(word)
假设我决定输入字母we
,这些字母有 2 种可能的排列方式, ew
和we
。 这两个都是我正在使用的字典 api 中的单词,但是如果您运行上面的代码,它只会附加ew
。
但是,如果您从test
删除ew
,使其只有we
,它将附加we
。
这里发生了什么?
注意:这里是 api 的 GitHub: https : //github.com/meetDeveloper/googleDictionaryAPI
希望这可以帮助!
import requests
test=["we","ew"]
liste=[] #Redeclaring the list inside the loop will eliminate previously stored values and as such the scope of the list should be outside the loop.
for word in test:
parameters={"define":word}
r=requests.get("https://googledictionaryapi.eu-gb.mybluemix.net",params=parameters)
if r.status_code==200:
liste.append(word)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.