Sal's Shipping: issue with syntax

hi, I’m workign on the Sal’s shipping project, but I’m having an issue; when I print this code, nothing come out. here’s the code I’m trying to print:

weight = 3


if weight <= 2:

 cost_ground = weight * 1.50 + 20

elif weight <= 6:

 cost_ground = weight * 9 + 20

elif weight <= 10:

 cost_ground = weight * 4 + 20

else: 

 cost_ground = weight * 4.75 + 20

 print(cost_ground)

the weird thing is, I tried to write the code again manually, but the other one gets printed with no issue whatsoever. here’s the other code:

weight = 3

if weight <= 2:

 cost_ground = weight * 1.50 + 20

elif weight <= 6:

 cost_ground = weight * 3 + 20

elif weight <= 10:

 cost_ground = weight * 4 + 20

else:

 cost_ground = weight * 4.75 + 20

print (cost_ground)

I don’t get it: what’s the difference? I also noted that in the Youtube video for the project the author speaks about functions, but at the point this project comes around for me (after the Magic-8-ball lesson) I haven’t studied them yet. Is that normal?

Indentation matters in python. It’s weird if you’re coming from C-style languages but you get used to it. (I personally prefer a tab or 4 spaces to differentiate better in my own code).

So your first example the print statement belongs to the conditional else branch (so it will not run if that branch is not reached). The second example it runs unconditionally.

1 Like

Thank you for the reply!