Python 如何向API调用添加范围搜索条件?

Python 如何向API调用添加范围搜索条件?,python,api,Python,Api,我想询问客户每顿饭有多少卡路里,然后使用该标准进行搜索。我不确定如何将输入集成到范围搜索中 导入请求 def配方搜索(成分): 配方_appid='0f89098e' 配方_appkey='80a8b7c8361daa22182bc3b3eb9f277e' url='1〕https://api.edamam.com/search?q={}&app_id={}&app_key={}&carries={}。格式, 食谱(卡路里) response=requests.get(url) data=res

我想询问客户每顿饭有多少卡路里,然后使用该标准进行搜索。我不确定如何将输入集成到范围搜索中

导入请求
def配方搜索(成分):
配方_appid='0f89098e'
配方_appkey='80a8b7c8361daa22182bc3b3eb9f277e'
url='1〕https://api.edamam.com/search?q={}&app_id={}&app_key={}&carries={}。格式,
食谱(卡路里)
response=requests.get(url)
data=response.json()
返回(数据['hits'])
def run():
配料=输入('先按日期使用什么配料?')
卡路里=输入(“你每顿饭有一个期望的估计卡路里摄入量吗?”)
结果=配方搜索(配料)和配方搜索(卡路里带)
运行()

您可以使用默认值将第二个参数添加到
recipe\u search
,因此您不必强制将其指定给该方法

def recipe_search(ingredient, calories=500):
    recipes_appid = '0f89098e'
    recipes_appkey = '80a8b7c8361daa22182bc3b3eb9f277e'
    url = 'https://...={}'.format(ingredient, recipes_appid, recipes_appkey, calories)
然后调用
results=recipe\u search(配料、卡路里含量)


简化一点边界检查,您可以

def run():
    ingredient = input('What ingredient is used by date first?')
    calories = input('Do you have a desired estimated calorie intake per meal?') or 200
    lower_bound = 100
    upper_bound = 300
    calories = min(max(int(calories), lower_bound), upper_bound)
    results = recipe_search(ingredient, calories)
    for result in results:
        recipe = result['recipe']
        ...

谢谢-超级清晰和简单。唯一的问题是它返回错误:TypeError:recipe_search()接受1个位置参数,但2个位置参数被给定了如何使用两个位置参数进行搜索?@CTheFridge是否更新了recipe_search方法?正如我在代码开头解释的那样?完美-谢谢完美-谢谢完美-谢谢