Python Development Course
Chapter
>
Level
Conditions
Checking for existence
Objective
Find if ingredients stored in containers and combine them to make food.
You need several ingredients to be able to make a salad but you need to collect them first. You’ll find carrots, tomatoes, lettuce and cucumbers in the field, carrots and cucumbers however are stored away. Collect the items you can grab in the field.
In order to find the missing items head to the X marks in front of containers and test conditions to check if the items are stored in a container. Use a for loop to make the process quicker and the collect() function to retrieve any items.
for x in range(4): ....... if "carrot" or "cucumber" in bag: player.collect(bag[x])
Once you collect all items, store them inside a list named items and store the number of each item collected using append() .
Finally, walk to the light X mark and combine all items for a meal using the combine() function. Check if all items are accounted for by checking the length of your list in an if statement by using the operator len() which checks the length of your list. The > or < can be used to check if a value is larger or smaller than a specific value. The implementation is: if len(items) < 5: