A little extension of functionality on the Sals shipping task!

Whilst doing this task in the Analyze Financial Data with Python skill path, I felt that there was ample opportunity to further the programme with my limited knowledge. I added some extra functionality and would appreciate some feedback on the code! TIA

Nice job!

I would try factoring out lines of code that repeat themselves, it helps to: read, debug/test, adjust, and scale.

One example

if weight <= 2:
elif weight > 2 and weight <= 6:
elif weight > 6 and weight <= 10:

happens twice and takes up about 15 lines.

One example of what you can do: write a function calc_shipping() that takes in a weight class and returns the values the values for any type of shipping you want. This implies that the weight classes are also abstracted out into another function calc_weight_class().

So then when it’s executed it would look like this:

item_weight_class = calc_weight_class(item)
cost_options = calc_shipping(item_weight_class)

The execution code itself already describes what is happening, so there’s less need for comments and acrobatics from the reader (and when you come back to your code it’ll be easier to read).

The other benefit is adaptability, maybe you want to take weight class to figure out how much you can fit in a truck/plane/drone… then you have a function for that already! (rather than repeating all the conditionals).

This is a very loose example, but it’s just something to think about (I’m not sure functions are even introduced yet by Sal’s shipping). But it’s always nice to come back to old exercises and improve upon them and make them your own.


Thanks for the tips, much appreciated!