Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. import requests
  2. import json
  3.  
  4.  
  5. class Ingredient:
  6. def __init__(self, name, measurement):
  7. self.name = name
  8. self.measurement = measurement
  9. def __str__(self):
  10. if (self.measurement is None):
  11. return self.name
  12. return self.name + ", " + self.measurement
  13.  
  14. url = "https://www.thecocktaildb.com/api/json/v1/1/search.php?s=margarita"
  15. response = requests.get(url)
  16. data = response.json()
  17. cocktails = data["drinks"]
  18.  
  19. for cocktail in cocktails:
  20. print(cocktail["strDrink"])
  21. ingredients = []
  22. x = 1
  23. while (True):
  24. strIngredient = "strIngredient" + str(x)
  25. strMeasure = "strMeasure" + str(x)
  26. name = cocktail[strIngredient]
  27. measurement = cocktail[strMeasure]
  28. if (name is None):
  29. break
  30. else:
  31. ingredients.append(Ingredient(name, measurement))
  32. x += 1
  33. print(' '.join(map(str, ingredients)))
  34. print("\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement