Sal's Shipping: I wrote this code for the project and want to know if I am correct


So I wrote this code for the sal’s pizza project. I want to know if it is correct. Im not sure because you can check the each task with out knowing if you did them correctly. I believe the code works…

Nicely done! There are a few small problems, and improvements I can point out, but all in all your logic is on, and you did a great job.

The first thing I noticed, is the way you are checking conditions:

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

You use and and in you elifs. There is no need to check if the weight is more then the previous measurement, since the if would have run if it wasn’t. You only need to check if the weight is less than the next amount:

if weight <= 2: 
  #this will run if the amount is less than or equal to 2
elif weight <= 6:
  #this will run if the amount is more than 2
  #this will run if the amount is more than 6

This is not an error, but is extra code and can be removed to simplify the program.

The second thing I saw is some extra returns?

def ship_ground(weight):
  if (weight <= 2):
    return weight*1.50 + 20.00
  elif (weight > 2) and (weight <= 6):
    return weight*3.00 + 20.00
  elif (weight > 6) and (weight <= 10):
    return weight*4.00 + 20.00
    return weight*4.75 + 20.00
  return cost      
#   \
#   This return will never run

Since there is a return in ever stage of the if/elif/else block, that last return will never run. Even if it did though, it seems cost is undefined for the first function, and exists in the global scope for the second function:

cost = drone_ship(weight)

The last thing I saw was an error on line 36 inside the if block:

if (ship_ground(weight)) > (drone_ship(weight)):
  print(" Drone Shipping is the best option. It will cost: " + str(drone_ship(weight) + "."))

Take a look at how your parenthisis are placed. Currently when this line runs you get a TypeError:

TypeError: unsupported operand type(s) for +: 'float' and 'str'

Last thing I wanted to recommend to you is learning f string interpolation. Codecademy doesn’t teach this, I believe because it is newer than their lessons. As of Python 3.6, you are able to put a variable directly into a string without converting the variable with str():

num = 55.6

#          The f denotes a string with interpolation
#         /
string = f"{num} is a number!"
#          \
#           The curly brackets indicate a variable.

This can save a lot of time when working with many variables you are displaying as part of a string.


Thanks for the feedback. I really like working through this on my own but, realized pretty quick that just because I can make it work doesn’t mean its correct/could be better.

1 Like