'Using a function to determine weather a list and a tuple both with variables match
i have a tuple within a tuple:
meals = (
\# name of dish \[0\], ingredients \[1\]
("fish_sticks", ("frozen fish", "potatoes", "mustard")),
("chicken_curry", ("chicken", "curry paste", "carrots", "potatoes", "rice")),
("chicken_veg", ("chicken", "potatoes", "carrots")),
("pasta", ("spaghetti", "tomato sauce"))
I have to return a true/false depending on if what I have in my fridge = the meal, to see if I can make it.
This is where I started:
def meal_list(meal, ingridients_list):
for item in meal:
if meal == ingridients_list:
print("true")
else:
print("false")
return meal_list("fishsticks",("frozen fish", "potatoes", "mustard") )
I do realize that I have no list to show the code what is right and wrong. just dong know how to get going.
Solution 1:[1]
You have to check if ALL ingredients needed to make the meal are in your ingridients_list. Currently, you print immediately after the first iteration of the loop (which isn't structured correctly).
Try:
def meal_list(meal, ingridients_list):
#get all the ingredients for the meal
needed = [i for m, i in meals if m==meal][0]
#check if items needed are in your ingredients
for item in needed:
if item not in ingridients_list:
return False
return True
>>> meal_list("fish_sticks",("frozen fish", "potatoes", "mustard"))
True
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
