Python Development Course
Chapter
>
Level

Conditions
Complex if statement

Objective

Grab items in the field and combine them by using condition operators.

Operators allow you to perform more complex conditions and with less code. Grab all the items in the field and store them in a list just like in the previous exercise.

stash = ["milk", "grape", "strawberries", "red berries", "empty jar"]

Once you grab all the items, head for the X marks and combine them by using the and and or Boolean operators. Using an if statement, single out the items in the list and check if they’re inside the list using the in operator.

if "item 1" and "item 2" in stash: player.combine(stash) if "item 3" or "item 4" and "item 5" in stash: player.combine(stash)

The first code checks if "item 1" and "item 2" are inside the stash, if so it combines them. The second code checks if either "item 3" or "item 4" is present along with “item 5” in the stash, if the conditions are met then the combine() code is run.

Use the if statement along with operators to combine items in the stash. On the top X mark, check if “grape” and “empty jar” are in the stash and combine them to make Grape Juice. On the bottom light X mark check if you have either “strawberries” or “red berries” along with "milk" and combine them to make Berry Milk.

Code book