# Get stuck on a quiz question—need some clarification on if statement

Question is:

def print_something(x):
if x <= 2:
print(“This is printed”)
if x <= 4:
print(“This is also printed”)
if x <= 6:
print(“Is this printed?”)
if x <= 8:
print(“This might be printed.”)

print_something(5)

I chose option B ‘Is this printed?’ But it has shown the correct answer is ‘Is this printed? and This might be printed.’ I’m confused. Should this program end running once it fits x <= 6? Why does it keep running to the last line of the function? If this were true, then why the cost of the shipment issue turn out differently? That said, once the weight is over 10 kg, we only take price at the highest level without running down the function to change the price.

For your reference, I paste the cost code here.

def drone_shipping(weight):
flat_charge = 0
if (weight > 10):
price = 14.25
elif (weight > 6):
price = 12.00
elif (weight > 2):
price = 9.00
else:
price = 4.50
cost = price*weight + flat_charge
return cost
print(drone_shipping(1.5))

Could someone help me out?

Welcome to the forums!

Correct me if I’m wrong, but a function doesn’t stop executing until a) everything has been completed or b) it returns something. So:

``````def print_something(x):
if x <= 2:  #5<=2 == False
print("This is printed") # not executed
if x <= 4: #5<=4 == False
print("This is also printed") # not executed
if x <= 6: #5<6 == True
print("Is this printed?") # (executed), but not returned, so your function keeps running
if x <= 8: #5<8 == True
print("This might be printed.") # executed as well
#everything is done executing, so the function stops running
print_something(5)
>>> "Is this printed?"
>>> "This might be printed."
``````

Now if you wrote your function like this:

``````def print_something(x):
if x <= 2: #5<=2 == False
return "This is printed" # not executed
if x <= 4: #5<=4 == False
return "This is also printed" # not executed
if x <= 6: #5<6 == True
return "Is this printed?" # (executed), and returned, so function ends here.
if x <= 8: # never evaluated
return "This might be printed."

print(print_something(5))
>>> "Is this printed?"
``````

Notice how it will only return “Is this printed?”. This is because your function stopped running as soon as the if condition `if x <= 6:`was evaluated `True` and returned “Is this printed?”

Your function never checks your `weight` parameter against your other elif conditions, because the first if condition is True. If they aren’t executed, that means price won’t be changed.

1 Like

As well as what @h1lo said, it is due to the fact the first function is using chained `if` statements, whereas the second function is using `if…elif`.
When you chain `if` statements, each one can execute-they are independent of one another. However, when you use `if…elif`, the statements are dependent on each other. That means the `elif` will only run if the `if` is `False`; like `if…else` statements:

``````if 1 == 1:
print("Yes")
if 2 == 2:
print("Yes, again.")
if 3 == 3:
print("Also yes.")
#output:
>>"Yes"
>>"Yes, again"
>>"Also yes"
``````

This is because the `if` statements happen independently of one another.

``````if 1 == 1:
print("This is printed")
elif 2 == 2:
print("Is this printed?")
elif 3 == 3:
print("Should this print?")
#output:
>>"This is printed"
``````

This is because while `2==2` and `3==3` are `True`, they are never evaluated as the first expression (in the `if` clause)-`1==1` is `True`. Once that runs, the rest of the `elif`s don’t run. Similarly:

``````if 1 == 2:
print("1 == 2")
elif 1 == 1:
print("1 == 1")
elif 2 == 2:
print("2 == 2")
#output:
>>"1 == 1"
``````

This is because the first expression (`1 == 2`) is `False`, but the second (`1 == 1`) is `True`, so the last one (`2 == 2`) never runs.

I hope this helps!

3 Likes

Thank you for your help, h1|o:)

I agree with you on the return part. What I don’t understand is, in weight code, other elif statements are not executed even if they are true. Say, let’s make weight as 14. Then it fits the first if, the price would be 14.25. Even it still fits the second elif, we don’t change the price based on that. I assume this rule applies to the tested question.

Aha, so if statement and elif ones are totally different. In latter case, the program won’t keep running down once the argument fits the situation as opposed to the former case. I will keep that on mind. Very much appreciate your help, codeneutrino!

It’s basically what @codeneutrino said.

The first if condition checks if weight is greater than 10 (which is true, because 14>10). Because the first `if` is true, the `elifs` never run.

2 Likes

Wow, thanks for your speedy response. Now I see the differences. Have a nice day:D

1 Like