Sal's Shipping Project Troubles


#1

I had a ton of trouble with the syntax for this project. I watched the video for the project walkthrough, but felt very uncomfortable with the fact that the solution was so different than anything I’ve seen in the lessons so far. In the end, because I couldn’t get my own method to work, I copied the method in the video with very slight modifications. The code runs well now, but each time it prints I see an extra line that says “None”. Please help me figure out why this is happening and how I can fix it! Thank you so much :slight_smile:


#2

Hi @kgnunn999,

The extra None is not your fault. Due the to manner in which Codecademy handles submitted code for this exercise, the return value of the cheapest_shipping function gets displayed. Since we have not explicitly supplied a return value, it defaults to None.

You could add a return statement as the final line in the function. Try this as an example, making sure it is indented appropriately to be part of the function, but not part of the else block …

  return "Thanks for choosing Sal's Shipping!"

You could do this instead …

  return cost

FAQ: Code Challenge: Loops - Greetings
#3

It would be interesting to see your code that didn’t work. That at least may help us arrive at some talking points rather than critically analyzing the given solution.

Sal’s Shipping

Please post your unaccepted code, and any error messages that were generated by it. I think you could get something more from this question by doing so.


Silliness segue…

def ground_shipping(weight):
  return 20 + g_rates[tabs.index([x for x in tabs if weight > x][-1])] * weight

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

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)))
53.60
6.75

See any repetition in the above? It’s my usual harp, avoid duplication and repetition, so this means a helper function is in good order.

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

so that,

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

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

Funny… The only function to contain an if statement is the helper. A major paradym shift in control flow. This is not elementary code. Far from it; but, you now have experienced a different view of how DATA can be processed with minimal logic as long as we set up the criteria and constraints. In a sense this program is merely an inverse or yours.


One thing above that rubs me the wrong way is the number literal inside the ground_shipping function. We can replace that with a parameter, or with a global. In this instance, since all the data is global, we may as well add this to the global data pool.

base_rate = 20
  return base_rate + g_rates[get_rate_index(weight)] * weight

In case you’re wondering, the work we’ve been doing here is called, refactoring. We could not have got here without the naive code. Stick with that model while you learn. I mean it. Ignore (but bookmark) this example until you are done this course, then come back to it.

For now, we still wish to see what attempts you have made, thus far…

@alyssavigil if we could get coach input here it would be invaluable. Someone needs to explain the value of naive code.


#4

Thank you! I’ll give that a shot!


#5

Wow, my head is spinning! I’ll definitely have to come back to this one at a later date to see if I can understand it. As for the original flawed code of mine, I’ll have to restart the project and try to remember what it is I did. I would like to know why it didn’t work…


#6

Was your code not accepted by the SCT? Did it raise any errors? What was it doing/not doing according to expectations?

As for my example above, ignore it until you finish the unit on Advanced Topics.


#7

Since this is a project rather than a lesson, Codecademy does not subject it to an SCT. However, the Python interpreter could, of course, raise an error, if something is wrong.


#8

Thank you so much for this I’ve been tearing my hair out trying to figure it out!