Sal's Shipping - Final Step Syntax Error

Hi all - new to CodeAcademy, this is my first question! Please can you explain why I am getting a syntax error at the final step of the Sal’s shipping project (when creating the function to print the cheapest method and how much it costs)?

I used a slightly different method to the method used in the GetHelp video (I also embedded ifs/elses in the conditions) so I would like to understand why my method also wouldn’t work.

My code is here:

Also reproduced below for convenience:

def ground_cost(weight):
if weight <= 2:
ground_cost = 20 + (weight * 1.50)
elif weight <= 6:
ground_cost = 20 + (weight * 3.00)
elif weight <= 10:
ground_cost = 20 + (weight * 4.00)
ground_cost = 20 + (weight * 4.75)
return ground_cost


premium_ground_cost = 125

def drone_cost(weight):
if weight <= 2:
drone_cost = weight * 4.50
elif weight <= 6:
drone_cost = weight * 9.00
elif weight <= 10:
drone_cost = weight * 12.00
drone_cost = weight * 14.25
return drone_cost


def user_choice (weight):

ground = ground_cost(weight)
drone = drone_cost(weight)
premium = premium_ground_cost

if ground < drone:
if ground < premium:
print:("You should use ground shipping. It will cost you " + str(ground))
print:("You should use premium ground shipping. It will cost you " + str(premium) + “.”)

elif drone < ground:
if drone < premium:
print ("You should use drone shipping. It will cost you " _ str(drone) + “.”)
print ("You should use premium ground shipping. It will cost you " + str(premium) + “.”)


Thank you so much!


The error is in this line. You see that underscore [ _ ], remove it and add a addition symbol [+].


Thanks @snikhill - super helpful (and slightly embarassing that I missed that). I deleted the underscore/fixed the error but weirdly keep getting ‘none’ where the output should be for that particular call in the terminal. Know why this might be?

If your function is returning None it’s worth noting that a function without a return statement will always return None.

Regarding your previous error it’s worth noting the error code provided. It can often help point you to the issue. If you’ve followed it up and are still unable to fix then consider asking for assistance but please include the error code itself (it makes it much easier to find).

Thanks @tgrtim - that’s actually a super helpful tip for the books. I didn’t realise that. I replaced all the ‘prints’ within the function with ‘returns’ and the code worked.

I am also slightly confused as to why this is - I remember specifically using ‘print’ rather than ‘return’ within the function during the exercise in Slide 11 (Else If Statements) of the Control Flow Lecture and the code (below) worked to produce the correct statements when it was called. Odd.

1 Like

Hello again,

You have added 2 print() statements in the end. Now the thing is that, user_choice() is already printing the value.
So, print(user_choice()) becomes obsolete.

The reason None is being printed because there is nothing to print.
Now, to replace that None, the function user_choice() needs to return something (which in this case is: NOTHING).


Sorry, I just saw this. I had already written my previous reply.

No worries - all helpful. Thank you!

Print statements in a function still execute. Just be careful about using them and using print(func()) or you’ll wind up with some odd results. With a quick copy/paste I get the following output-

Your grade is a B
Your grade is an A
Your grade is a D

Thanks @tgrtim. Yeah - I got that result too. I guess I was trying to understand why they executed in this case (albeit in a weird way, with none interspaced between the statements) but they didn’t execute at all in my user_choice function from the shipping project.

Ah. Took a look at the shipping costs project and there are some syntax issues that mean your print statements never actually execute as you intended.

print ()
# that gap means print isn't actually called
# a colon indicates indentation should follow
# to be honest I'm not 100% sure how it works but the function definitely isn't called

That makes sense!

Oddly however, as we speak I am doing a Control Flow quiz and this question popped up. The print statements execute with no colon (and there is also no return statement in the function). Unsure how to accord this with everything we’ve just discussed haha!

Consider the code below. What would this print to the terminal?

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.”)


The correct answer is that the terminal will print “Is this printed?” and “This might be printed” (which is to be expected and not controversial) - why does the print statement execute here with no return in the function and no colon?

Apologies I actually meant that the colon meant your print statement wouldn’t work. It’s just a function call: func(). Having a space or other operator between the function name and the parantheses means the function won’t be called as expected.

print("a")  # is fine
print ("a")  # print doesn't actually get called
print:("a")  # print doesn't actually get called

There’s nothing explicitly wrong with having print statements within a function. It’s also acceptable to have a function which doesn’t use a return statement. The issue is when you try to print the output of a function without a return statement. That will print None. In your given example-

# this would execute the function but also print `None` at the very end

I hope that makes a little more sense.


All understood - this definitely clears it up. Thank you.