Sal's Shipping

My code looks like this.

my output look looks this

C:\Users\Shop\PycharmProjects\untitled\HelloWorld\venv\Scripts\python.exe C:/Users/Shop/PycharmProjects/untitled/HelloWorld/app.py
53.6
6.75
The shipping of 4.8 pound is ground shipping method and will cost 34.4 The shipping of 41.5 pound is ground shipping method and will cost 217.125

Process finished with exit code 0

can somebody help me by telling me why i am not getting the expect output .
The shipping of 41.5 pound is ground shipping method and will cost $ 125

With an input of 41.5, costground(weight) is 217.125 and droneshipping(weight) is 591.375, so
costground(weight) is less than droneshipping(weight)
and
the if-elif string in cheapshipping(weight) stops (i.e., returns True) at

  elif costground(weight) < droneshipping(weight):
    return "ground shipping method and will cost $ " + str(costground(weight))

… which, as we have seen, returns 217.125

1 Like

the problem is inside your cheapshipping function so I suggest to try to implement your function that can test to one thing out of the remain one and you all fix your problem
because your function test one thing out of one at a time and that is not good so if your can make it check one thing out of the remain one will be perfect and and correct your unexpected answers.

Alex.

https://www.codecademy.com/paths/computer-science/tracks/cspath-flow-data-iteration/modules/dspath-control-flow/projects/sals-shipping

This may be out in left field…

return min([premiumweight, costground(weight), droneshipping(weight)])

Mind it only returns a value, with no label so that’s no good. It’s the old adage,

Do you want it good, fast or cheap?
Cheap won’t be good or fast.
Fast won’t be cheap or good.
Good won’t be fast or cheap.

What if we compute the values, first, then compare then in the return statement (with a ternary)?

  ground = ground_shipping(weight)
  drone = drone_shipping(weight)
  return ('Premium', premium) if premium < ground or premium < drone else ('Ground', ground) if ground < drone else ('Drone', drone)

We would output the return like so,

method, cost = lowest_cost(41.5)
print ("{}: {:.2f}".format(method, cost))

hello, I keep getting an the error message below for the code attached. Is the string interpolation outdated?

6.75
Traceback (most recent call last):
File “script.py”, line 50, in
print_cheapest_shipping_method(4.8)
File “script.py”, line 31, in print_cheapest_shipping_method
ground = shipping_cost_ground(weight)
TypeError: ‘float’ object is not callable

def print_cheapest_shipping_method(weight):
  
  ground = shipping_cost_ground(weight)
  premium = shipping_cost_ground
  drone = shipping_cost_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 shipping method is $%.2f with % shipping." 
    % (cost, method)
  )

This error message says that a float object is being treated like a function and it has been invoked. What float object would this be? Check the trace for the line where this call is being attempted.

You see where the premium assignment is actually a reference to the function that ground uses?

That may not be the problem that raised the exception, but will certainly cause some problems once it is encountered during runtime. Clearly, shipping_cost_ground has been corrupted somewhere and turned from a function to a float. Be very careful with naming and give everything a unique, self-describing name.

Thank you for taking a look a my code! You’re spot on, I overlooked the name of my premium variable and didn’t recognize the naming conflict with the previous function. I’ll make sure to pay closer attention next time. Thanks.

1 Like

You’re welcome. Don’t mind the lecture, it’s my usual style.

One thing I try to do is not let data objects have names that resemble a function name (which includes built-ins). A reader can spot data variables from function names at a glance. This helps prevent overwriting a function is a data object.

Take for instance this utiility function…

>>> def is_float(x):
    try:
        x = float(str(x) + '.0')
        return False
    except ValueError:
        return True

>>> is_float(0.0)
True
>>> is_float(1)
False
>>> 

Knowing that we have a function such as this in our arsenal, we would not name a variable, is_float, else it would colide with our function name.

num = 0.0
num_is_float = is_float(num)
print (num_is_float)    # True