Sal's Shipping: Syntax Error and Trouble with Output

Hi All,
I’m new to programming and I’m stuck on the Sal’s Shipping program, I’m in a bit of a pickle not sure how to exactly write from the table into python code! So I’m stuck with a syntax error and its been a while! I know my code is really basic but I need your help in making it better and printing the ground shipping to the foreground. I really aim to perservere, but this is giving me a headache!
Very much appreciate your help :slight_smile:

weight = 0

# Ground Shipping

if weight <= 2:

  ground_cost = weight * 1.5 + 20

elif weight <= 6:

  ground_cost = weight * 3.0 + 20

elif weight <= 10:

  ground_cost = weight * 4.0 + 20

else weight >= 10:

 ground_cost = weight * 4.75 + 20

print(ground_cost)
else weight >= 10: 

should be

else: 

(You shouldn’t write a condition for else.)

Although I guess you could do

else: # weight >= 10: 
1 Like

Hi @janbazant1107978602 Could you please explain to me why you should not write a condition for else?

The code after else runs when none of the previous conditions were True,
so the condition does not need to be written after else because the logic is handled by the previous if and elif conditions.

For example:

if x > 4:
  # code here runs if x > 4
else:
  # code here runs if we don't have x > 4

or, another example:

if y < 0:
  #// code here runs if y < 0
elif y == 0:
  #// code here runs if y is 0
else:
  #// code here runs if none of those previous conditions were true 
  #// meaning this block runs if y is not < 0 and y is not 0
2 Likes

@janbazant1107978602 :+1: