Python 3: Sal's shipping project

https://www.codecademy.com/courses/learn-python-3/projects/sals-shipping
I’ve copied the code from the walkthrough video but surprising still getting a Nameerror:
This is the message from the terminal:
Traceback (most recent call last):
File “script.py”, line 41, in
if ground < premium and ground < drone:
NameError: name ‘ground’ is not defined

but ground is defined on this line:
ground = shipping_cost_ground(weight)

Any help or guidance would be appreciated.

Difficult to say without seeing the full code, please copy paste the full code to the forum

have you considered scope rules?

1 Like
def shipping_cost_ground(weight):
 
  if weight <= 2:
    price_per_pound = 1.50
  elif weight <= 6:
    price_per_pound = 3.00
  elif weight <= 10:
    price_per_pound = 4.00
  else:
    price_per_pound = 4.75
  
  return 20 + (price_per_pound * weight)

print(shipping_cost_ground(8.4))

shipping_cost_premium = 125.00

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

print(shipping_cost_of_drone(1.5))



def print_cheapest_shipping_method(weight):

  ground = shipping_cost_ground(weight)
  premium = shipping_cost_premium
  drone = shipping_cost_of_drone(weight) 

if ground < premium and ground < drone:
    method = "standard ground"
    cost = ground
elif premium < ground and premium < drone:
    method = "premium ground"
    cost = premium 
else: 
    method = "drone"
    cost = drone

print(
  "The cheapest option available is $%.2f with %s shipping."
  %(cost,method)
)
  
print_cheapest_shipping_method(4.8)
print_cheapest_shipping_method(41.5)

Did you miss this:

scope rules are indeed the problem, you define local variable in the function, and then you try to access these variable outside the body of the function.

1 Like

Your indentation is screwed up. You if, elif, and else statements should be inside your function

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


shipping_cost_premium = 125.00

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


def print_cheapest_shipping_method(weight):
  ground = shipping_cost_ground(weight)
  premium = shipping_cost_premium
  drone = shipping_cost_of_drone(weight)
  if ground < premium and ground < drone:
    method = "standard ground"
    cost = ground
  elif premium < ground and premium < drone:
    method = "premium ground"
    cost = premium 
  else: 
    method = "drone"
    cost = drone
    
print(
  "The cheapest option available is $%.2f with %s shipping."
  % 
  (cost, method)
)
  
print_cheapest_shipping_method(4.8)
print_cheapest_shipping_method(41.5)

I’ve made them into local variables which works but the print statement at the bottom comes up with this error:
“Traceback (most recent call last):
File “script.py”, line 48, in
(cost, method)
NameError: name ‘cost’ is not defined”

also, i have indented it properly its just the format in which it pastes.

1 Like

this answers your own question? The variable have a local scope, so these variable only exist within the body of the function. So trying to access these variable outside/after the function will give an error.

1 Like

A post was split to a new topic: My attempt at Sal’s Shipping

Did anyone else think this project was WAY over the scope of what was taught previously in the function lesson? Personally, it felt like the basic concepts that were introduced weren’t sufficient for carrying out that project without needing to research multiple other instructions on how to successfully complete it.

3 Likes

Hello @nbornstein!

For more practice on functions, you can check out this website. Otherwise, the community members here on the forums are very helpful if you have any specific questions.

1 Like

Thanks, mate for this code…I was stuck in the middle of the project but your code helped me to understand and solve this project so I really appreciate that you posted this here. Thanks again

Yeah… that doesn’t really refute his point that this task was poorly designed given that it asks far more of us than it has actually taught us. And the response that you can complete this lesson by doing exercises from a completely different website is not a good look.

Respectfully, I disagree. :slight_smile:

Completing Sal’s Shipping requires you to understand how to do the following:

  • Write a function declaration, which takes one non-keyword parameter and returns a value.
  • Implement conditional program flow using if..elif..else structures, with the appropriate comparison operators.

Both of these are covered in the Introduction to Functions and Control Flowlessons of the “Learn Python 3” course respectively. (I did them both before I wrote this.)

By the same logic, it would not be a good look for colleges to expect students to complete assignments or study on their own time outside of the lecture hall using material other than the lecture notes…

Yet, they do so because the majority of people do not cement a thorough and complete understanding of the lesson within the time they spend in the lecture listening to the prof.

In much the same fashion someone learning here with Codecademy could reasonably be expected to spend time practicing what they’ve learned not only with the practice materials provided here, but also with exercises and tasks from elsewhere like you’d do math problems from a textbook.

We frequently see people here asking questions like “How long does it take to complete {$chosen_course}?”, when this question is both wildly variable between learners as well as completely the wrong approach.

How much time anyone spends on learning the material will ultimately influence how easy or difficult, relatively speaking, they find it to apply what they’ve been taught.

Sure, Sal’s Shipping can be a tricky one to crack, because:

  • It’s a reasonably long project,
  • It compounds the concepts introduced in the previous three lessons, which comprise a decent sized amount of Python’s base syntax and functionality, and,
  • It asks you to implement some moderately complex comparisons, compared to the ones used in the “teaching” material of the prior lesson, with less granular instructions than a lesson would.

It’s not tricky because of a requirement to use features of the language sight unseen. There is nothing in this project you’re being asked to do that you haven’t been asked to do in the lessons prior, it’s just a bit more complex. In this case, the OP’s error appears to be related to scope - a topic covered in Exercise 10 of the “Intro to Functions” lesson. :slight_smile:

2 Likes

I went through the course myself with no prior coding experience and this project was fairly easy to complete for me. I am aware that everyone learns differently and that some may feel that it’s too big of a jump between the lesson and the project, so what point is there to refute?

The concepts used in this project have been taught in the course (the big ones being conditionals and functions). As I mentioned, you will most probably be able to find help here by posting a specific question (and any relevant code or screenshots).

I am not saying that you can complete this lesson by practicing on a separate website. Rather, completing those practices should increase your familiarity with functions and provide extra practice so you can more confidently use them in your code.

3 Likes

What’s more is we don’t have to abandon these lessons when they’re done. After we learn a few more neat concepts we can return and refactor the code.

def get_rate_index(weight):
  return tabs.index([x for x in tabs if weight > x][-1])

def ground_shipping(weight):
  return b_rate + g_rates[get_rate_index(weight)] * weight

def drone_shipping(weight):
  return d_rates[get_rate_index(weight)] * weight

def lowest_cost(weight):
  x = premium, drone_shipping(weight), ground_shipping(weight)
  y = min(x)
  return ('Premium', 'Drone', 'Ground')[x.index(y)], y

Now to implement them…

premium = 125.00
b_rate = 20.00
tabs = [0, 2, 6, 10]
g_rates = [1.50, 3.00, 4.00, 4.75]
d_rates = [4.50, 9.00, 12.00, 14.25]

print ("{:.2f}".format(ground_shipping(8.4)))
print ("{:.2f}".format(drone_shipping(1.5)))
print ("{}: {:.2f}".format(lowest_cost(4.8)[0], lowest_cost(4.8)[1]))
print ("{}: {:.2f}".format(lowest_cost(41.5)[0], lowest_cost(41.5)[1]))

And, with what we know about the new f-string,

print (f"{ground_shipping(8.4):.2f}")
print (f"{drone_shipping(1.5):.2f}")
print (f"{lowest_cost(4.8)[0]}: {lowest_cost(4.8)[1]:.2f}")
print (f"{lowest_cost(41.5)[0]}: {lowest_cost(41.5)[1]:.2f}")

We pick up a lot more in our reading than we ever do in lessons. If we never give that stuff a try and depend solely on the given material we are never going to make it in the real world. More than that, the more time we spend griping about the shortfalls of the course the more we block our vision from seeing anything else. Programming cannot be learned with blinders on or any preconceived bias or notions about how it should be taught. To learn we only have to apply ourselves.

4 Likes

You and @mtf bring up some great points on the learning process here on CC and its subsequent external exploration in the learning. I actually think the skill of utilizing the research tools available for a subject on the internet is as if not more important. I agree with it wholeheartedly.

However, while the forums and external resources are invaluable, there was also a lack of an acknowledgement of @nbornstein’s frustration he was voicing with the lesson in the original response, and it probably felt a little vague and more like a redirect away from their perspective. If @nbornstein feels like there is a way to improve the lesson, no matter if we agree with him or not, specific resources should be made known to him, i.e. #community:Feature-Course-Requests

Hello every one iam new to programming
i was trying to solve the shipping project and thats my code by i received error :

error
File “script.py”, line 22
return print(“you should ship using ground shipping it will cost” + str ground_ship(weight))
^
SyntaxError: invalid syntax

thanks

It looks like that line is missing some bits and pieces. You want to call str when you’re using it like this. For example str(3) would return the string '3'. It needs to be called with parentheses ( ) if you’re trying to convert a different data type to a string.

As an aside there’s little reason to mix print and return on this line. I’d suggest having the function use return only and just print the output when you call it outside the function, something along the following lines-print(function(arg)).