# Thank you mtf & Conditional Statement Question

#1

Here is my new code. Thank you mtf. Also, I have a question about if, else, and elif statements. If you have a if statement, is it mandatory to have a corresponding else and elif statement. Or is it okay to have one without the other. In my code I only put a if statement. Could you please explain what happens if stock[item] <= 0 since I don't have a else statement in my code (specifically).

shopping_list = ["banana", "orange", "apple"]

stock = {
"banana": 6,
"apple": 0,
"orange": 32,
"pear": 15
}

prices = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3
}

def compute_bill(shopping_list):
total = 0
for item in shopping_list:
if stock[item] > 0:
total += prices[item]
stock[item] -= 1

#2

When there is only a 'yes' / 'no' scenario, with only one action to perform,

``````if condition:
# action for True``````

Where there is an alternate action,

``````if condition:
# action for True
else:
# default action``````

When we are not bypassing any code, we can write,

``````if condition:
# code
if state:
# code
if something:
# code``````

In the above, each statement is independent, and all three are evaluated (no interruption in flow).

When there are multiple cases from which only one case can be true, we introduce, `elif`...

``````if condtion1:
# condition1 action
elif condition2:
# condition2 action
elif condtion3:
# condition3 action``````

Flow will only pass through one of the branches given a True condition. Unless there is a default action within the control flow, `else` is not required.

In any `if` statement, there is no limit on the number of `elif` cases, but there can only ever be one `else`, or default clause. `else` never appears before `elif`.