Ask coding questions

← Back to all posts
How can I fix my code so a search for football recipes returns a sorry no matches response
helpmicode (0)

https://replit.com/@CFGRecipes/Recipes-Program-V1#main.py

import requests

app_id = '79f381d8'
app_key = '6d0c821df084f53a1ac53d1217343795'

def main():
global ingredient
ingredient = input('Find something to cook! \n \nTo get started, tell me which ingredient you\'d like in your recipe? \n')
results = recipe_search(ingredient)
results_by_ingredients = sort_by_ingredients(results, True)
#for i in results:
#if ingredient not in i['recipe']:
#print('Sorry, I did not catch that')
#else:
show_results(results_by_ingredients)
if len(results_by_ingredients) == 0:
print('Sorry, no results! Please start again and try a different ingredient.')

def recipe_search(ingredient):
url = 'https://api.edamam.com/api/recipes/v2/?type=public&q={}&app_id={}&app_key={}'.format(ingredient, app_id, app_key)

response = requests.get(url)
recipes = response.json()
recipe_results = recipes['hits']
return(recipe_results)

def sort_by_ingredients(r, reverse_order):
s = sorted(r, key=lambda i: len(i['recipe']), reverse = reverse_order)
return(s)

def show_results(search_results):
for i in search_results:
name = i['recipe']
source = i['recipe']
url = i['recipe']
number_ingredients = len(i['recipe'])
calories = i['recipe']
servings = i['recipe']
calories_per_serving = int(calories / servings)

  print('{}'.format(name))
  print('{} ingredients'.format(int(number_ingredients)))
  print('{} calories per serving'.format(calories_per_serving))
  print('View the recipe via {}: {}\n'.format(source, url))

  with open('recipes.txt', 'a+') as recipe_text_file:
    recipe_text_file.write('{}\n'.format(name))
    recipe_text_file.write('{} ingredients\n'.format(int(number_ingredients)))
    recipe_text_file.write('{} calories per serving\n'.format(calories_per_serving))
    recipe_text_file.write('View the recipe via {}: {}\n\n'.format(source, url))

main()

Comments
hotnewtop
Coder100 (18915)

please send the repl instead of copying and pasting your code.